2011-12-14 69 views
3

我正在爲我的Wordpress主題創建一個幻燈片短代碼,但遇到了一個小問題。這是簡碼的樣子:如何獲取嵌套短代碼中的屬性?

[slideshow width=500] 
    [slide]http://example.com/image1.jpg[/slide] 
    [slide]http://example.com/image2.jpg[/slide] 
    [slide]http://example.com/image3.jpg[/slide] 
[/slideshow] 

所以,基本上是兩個不同的簡碼(幻燈片和幻燈片),我需要設置每一個「滑」簡碼的寬度。如何從父級「幻燈片」簡碼獲取「寬度」屬性並將其傳遞給每個孩子的「幻燈片」?

//Create slideshow wrapper div 
    function shortcode_slideshow($atts, $content = null){ 
     $return = '<div class="slideshow">'; 
     $return .= do_shortcode($content); 
     $return .= '</div><!-- end slideshow -->'; 

     return $return; 
    } 

    //Create each slide HTML 
    function shortcode_slide($atts, $content = null){ 
     $return = '<a class="dolightbox" href="'.$content.'">'; 
     $return .= '<img src="'.$content.'" /></a>'; 
     return $return; 
    } 

    add_shortcode('slideshow', 'shortcode_slideshow'); 
    add_shortcode('slide', 'shortcode_slide'); 

回答

1

結束使用全局變量將值傳遞到第二個短代碼函數。我想也許有一個原生的WordPress的做法,但我顯然不是。

//Create slideshow wrapper div 
$globalWidth = NULL; 

function shortcode_slideshow($atts, $content = null){ 
    extract(shortcode_atts(array('width' => ''), $atts)); 
    global $globalWidth; 
    $return = '<div class="slideshow">'; 
    $return .= do_shortcode($content); 
    $return .= '</div><!-- end slideshow -->'; 

    return $return; 
} 

//Create each slide HTML 
function shortcode_slide($atts, $content = null){ 
    global $globalWidth; 
    $return = '<img width="'.$globalWidth.'" src="'.$content.'" />'; 

    return $return; 
} 

add_shortcode('slideshow', 'shortcode_slideshow'); 
add_shortcode('slide', 'shortcode_slide'); 
+0

我想,我們需要在第一個函數中添加`$ globalWidth = $ width;`。 – 2018-01-28 10:24:04