[英]Retrieve wp_terms by category_name
I'm trying to limit the retrieved testimonial (using get_terms
) by their category names. 我正在尝试通过类别名称来限制检索到的推荐(使用
get_terms
)。 category_name = "xxx" doesn't seem to do anything so I'm at a loss. 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' isn't a valid argument for get_terms. “ category_name”不是get_terms的有效参数。 The WP reference documentation here lists the acceptable arguments.
此处的WP参考文档列出了可接受的参数。 The one you want is:
您想要的是:
'name' : (string|array) Optional. 'name' :(string | array)可选。 Name or array of names to return term(s) for.
要返回其术语的名称或名称数组。
So, assuming $cat is the name you want to search for, try changing your get_terms arguments to: 因此,假设$ cat是您要搜索的名称,请尝试将get_terms参数更改为:
$terms = get_terms( array(
'taxonomy' => 'testimonial',
'name' => $cat,
'hide_empty' => true,
) );
Update: 更新:
get_terms
only returns information about the term you searched for, as per your original question. 按照原始问题,
get_terms
仅返回有关您搜索的术语的信息。
To get all posts associated with a term, you need to use get_posts
and tax_query
as follows: 要获取与术语相关的所有帖子 ,您需要使用
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
))
));
Reference: get_posts documentation in WP Codex 参考: WP Codex中的get_posts文档
category_name is not a valid arg for get_terms, try 'name' instead: 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.