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]

したがって、基本的には 2 つの異なるショートコード (スライドショーとスライド) であり、各「スライド」ショートコードの幅を設定する必要があります。親の「スライドショー」ショートコードから「幅」属性を取得し、それを各子「スライド」に渡すにはどうすればよいですか?

    //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');
4

1 に答える 1

1

グローバル変数を使用して値を2番目のショートコード関数に渡すことになりました。それを行うためのネイティブの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');
于 2011-12-15T18:34:05.300 に答える