【问题标题】:wordpress - adding shortcode with tax_query value terms variablewordpress - 添加带有 tax_query 值术语变量的简码
【发布时间】:2015-04-29 04:04:54
【问题描述】:

我正在为 WP 编写一个简码函数,但我有一个问题,我不明白当我编写简码时如何直接在 wordpress 中调用变量,就像这样 [shortcode-name terms="news1" ]

这是我的代码,目前我在查询中传递了 terms='news1',但我想将此值转换为一个变量,这样我就可以像这样 [shortcode-name terms="新闻1"]。

在查看了各种 tuts 之后,我需要将变量留空,但我无法让那部分工作......

有人可以指点我正确的方向吗?

function bxslider_shortcode() {
       $content = '<ul class="bxslider">';

       $loop = new WP_Query( array(
            'post_type' => 'bxslider',
            'posts_per_page' => -1,
            'post_status' => 'publish',
            'orderby' => 'date',
            'tax_query' => array(
                array(
                'taxonomy' => 'bxslider_category',
                'field'    => 'slug',
                'terms'    => 'new1',

                ),
            ),
            'order' => 'ASC'
        ));



         while ( $loop->have_posts() ) : $loop->the_post();

           $thumb_id = get_post_thumbnail_id();
           $thumb_url = wp_get_attachment_image_src($thumb_id,'full', true);
           $content .= '<li>';
           $content .= '<img src="';
           $content .= $thumb_url[0];
           $content .= '" alt="';
           $content .= get_the_title();
           $content .= '" /></li>';
         endwhile;

       $content .= '</ul>';
       echo $content;
     }

add_shortcode('bxslider', 'bxslider_shortcode');

【问题讨论】:

    标签: php wordpress shortcode


    【解决方案1】:

    您想将提供的 terms="SOMETHING" 有效地传递给 add_shortcode 中定义的函数吗?

    此页面中的示例 (http://codex.wordpress.org/Shortcode_API) 是

    // [bartag foo="foo-value"]
    function bartag_func( $atts ) {
        $a = shortcode_atts( array(
            'foo' => 'something',
            'bar' => 'something else',
        ), $atts );
    
        return "foo = {$a['foo']}";
    }
    add_shortcode( 'bartag', 'bartag_func' );
    

    所以对你来说可能是

    function bxslider_shortcode($atts) {
    $content = '<ul class="bxslider">';
    
    $loop = new WP_Query( array(
        'post_type' => 'bxslider',
        'posts_per_page' => -1,
        'post_status' => 'publish',
        'orderby' => 'date',
        'tax_query' => array(
            array(
                'taxonomy' => 'bxslider_category',
                'field'    => 'slug',
                'terms'    => $atts['terms'],
            ),
        ),
        'order' => 'ASC'
    ));
    
    while ( $loop->have_posts() ) : $loop->the_post();
    
        $thumb_id = get_post_thumbnail_id();
        $thumb_url = wp_get_attachment_image_src($thumb_id,'full', true);
        $content .= '<li>';
        $content .= '<img src="';
        $content .= $thumb_url[0];
        $content .= '" alt="';
        $content .= get_the_title();
        $content .= '" /></li>';
    
    endwhile;
    
    $content .= '</ul>';
    echo $content;
    }
    
    add_shortcode('bxslider', 'bxslider_shortcode');
    

    除非我错过了什么?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多