为特定短码的每个副本初始化Add_action()

时间:2013-10-10 作者:user2587741

我这里有一个生成滑块的短代码。在我尝试添加另一个具有不同选项的“partners”滑块之前,一切都正常。我无法为每个滑块指定设置,因为print\\u partners\\u scripts()函数只生成一次。我需要为每个在短代码选项中指定的滑块初始化print\\u partners\\u scripts()函数。我希望你明白我的意思;)

function print_partners_scripts() {
    global $extra_options, $pslider;
    echo \'<script type="text/javascript">\' . "\\n";
    echo \'jQuery(document).ready( function() {\'. "\\n";
    echo \'  $(".\'.$pslider.\'").bxSlider({
        slideWidth: 924,
        auto: 1,
        autoStart: 1,
        moveSlides: 1,
        minSlides: 7,
        maxSlides: 8,
        pager: false,
        controls: false,
        slideMargin: 5,
        autoHover: true
        \' . $extra_options . \'
        });\' . "\\n";
    echo \'});\' . "\\n";
    echo \'</script>\' . "\\n";
}

function partners($atts ) {
    global $extra_options, $pslider;
    extract(shortcode_atts(array(  
            \'ids\' => null,
            \'extra_options\' => null
        ), $atts));  

    add_action( \'wp_footer\', \'print_partners_scripts\' );
    $pslider = \'partners-slider\'.rand(100,999);
    $ids = explode( \',\', $ids );
    $output = \'<div class="section row-one"><div class="col ps-gradient"><div class="ps-wrap"><div class="\'.$pslider.\'">\';
    foreach($ids as $id) {    
    $img_attr = wp_get_attachment_image_src( $id, \'full\' );
    $alt_text = get_post_meta( $id, \'_wp_attachment_image_alt\', true);
    $output .= \'<div class="pslide"><a href="\' . $alt_text . \'"><img src="\' . $img_attr[0] . \'" /></a></div>\';    
    }
    $output .= \'</div></div></div></div>\';
    return $output;  
}  

add_shortcode(\'partners\', \'partners\');

1 个回复
SO网友:s_ha_dum

您可以通过添加自己的过滤器来传递值。概念验证:

add_filter(
  \'my_filter\',
  function() {
    return array(1,2,3);
  }
);

function print_partners_scripts() {
  $args = apply_filters(\'my_filter\',null);
  var_dump($args);
}
add_action( \'wp_footer\', \'print_partners_scripts\' );
您可以使用wp_localize-script 向其传递信息。

结束

相关推荐

Custom field in a shortcode?

我无法将图像从我的一个字段转换为短代码(它与get_the_post_thumbnail($post->ID, \'thumbnail\') 但我需要字段中的值udstiller logo.function logo_shortcode( $atts ) { extract( shortcode_atts( array( \'limit\' => \'50\', \'orderby\' => \'name\',