wordpress - 添加带有 tax_query value terms 变量的简码
wordpress - adding shortcode with tax_query value terms variable
我正在为 WP 写一个短代码函数,但是我有一个问题,我不明白当我写短代码时如何直接在 wordpress 中调用一个变量,就像这样 [shortcode-name terms="news1"]
这是我的代码,目前我在查询中传递了 terms='news1',但我想将这个值转换为一个变量,这样我就可以像这样调用短代码 [shortcode-名称术语="news1"].
在查看了各种 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');
如此有效,您想将提供的 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');
除非我错过了什么?
我正在为 WP 写一个短代码函数,但是我有一个问题,我不明白当我写短代码时如何直接在 wordpress 中调用一个变量,就像这样 [shortcode-name terms="news1"]
这是我的代码,目前我在查询中传递了 terms='news1',但我想将这个值转换为一个变量,这样我就可以像这样调用短代码 [shortcode-名称术语="news1"].
在查看了各种 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');
如此有效,您想将提供的 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');
除非我错过了什么?