2011-12-14 4 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

グローバル変数を使用して、値を2番目のshortcode関数に渡すことで終了しました。私はおそらくそれを行うネイティブ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;'を追加する必要があると思います。 –

関連する問題