![](/img/trans.png)
[英]how can i use “+” and “,” for category_name in WP_Query?
[英]Retrieve wp_terms by category_name
我正在嘗試通過類別名稱來限制檢索到的推薦(使用get_terms
)。 category_name =“ xxx”似乎什么也沒做,所以我很茫然。
function testimonial_shortcode( $atts ) {
$cat = $atts['cat'];
$testim='<div id="owl-demo" class="owl-carousel owl-theme">';
$terms = get_terms( array(
'taxonomy' => 'testimonial',
'category_name' => $cat,
'hide_empty' => true,
) );
foreach($terms as $custom_texonomy){
$imageid=get_option("testimonial_".$custom_texonomy->term_id."_testimonials__image");
$imgurl=wp_get_attachment_image_src( $imageid, 'full');
$testim.=' <div class="item">
...
}
add_shortcode( 'testimonialcat', 'testimonial_shortcode' );
“ category_name”不是get_terms的有效參數。 此處的WP參考文檔列出了可接受的參數。 您想要的是:
'name' :(string | array)可選。 要返回其術語的名稱或名稱數組。
因此,假設$ cat是您要搜索的名稱,請嘗試將get_terms參數更改為:
$terms = get_terms( array(
'taxonomy' => 'testimonial',
'name' => $cat,
'hide_empty' => true,
) );
更新:
按照原始問題, get_terms
僅返回有關您搜索的術語的信息。
要獲取與術語相關的所有帖子 ,您需要使用get_posts
和tax_query
,如下所示:
$myposts = get_posts(array(
'showposts' => -1, // get all posts
'post_type' => 'post', // change to whatever post type you want, or leave out if you want to get all post types
'tax_query' => array(
array(
'taxonomy' => 'testimonial',
'field' => 'name',
'terms' => $cat
))
));
category_name不是get_terms的有效參數,請嘗試使用“名稱”:
function testimonial_shortcode( $atts ) {
$cat = $atts['cat'];
$testim='<div id="owl-demo" class="owl-carousel owl-theme">';
$terms = get_terms( array(
'taxonomy' => 'testimonial',
'name' => $cat,
'hide_empty' => true,
) );
foreach($terms as $custom_texonomy){
$imageid=get_option("testimonial_".$custom_texonomy->term_id."_testimonials__image");
$imgurl=wp_get_attachment_image_src( $imageid, 'full');
$testim.=' <div class="item">
...
}
add_shortcode( 'testimonialcat', 'testimonial_shortcode' );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.