wordpress - 使用tax_query值术语变量添加短代码

时间:2015-02-26 16:55:08

标签: php wordpress shortcode

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

这是我的代码,目前我在查询中传递了条款='news1',但我想在变量中转换这个值,所以我可以在短代码中调用这样的代码[shortcode-name terms =“新闻1" ]。

在查看了各种各样的内容后,我需要将变量留空,但我无法使该部分工作......

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

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

1 个答案:

答案 0 :(得分:0)

所以你有效地想要将提供的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');

除非我错过了什么?