Как вы получаете атрибуты во вложенном шорткоде? - PullRequest
3 голосов
/ 14 декабря 2011

Я создаю шорткод слайд-шоу для моей темы 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]

Итак, в основном это два разных шорткода (слайд-шоу и слайд), мне нужно установить ширину каждого шорткода «слайда». Как получить атрибут width из родительского шорткода «слайд-шоу» и передать его каждому дочернему «слайду»?

    //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 Ответ

1 голос
/ 15 декабря 2011

Завершается использованием глобальных переменных для передачи значения во вторую функцию шорткода.Я подумал, что, может быть, есть собственный метод 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');
...