通过 category_name 检索 wp_terms
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 reference documentation here
列出可接受的参数。你想要的是:
'name':(字符串|数组)可选。 return 个术语的名称或名称数组。
因此,假设 $cat 是您要搜索的名称,请尝试将您的 get_terms 参数更改为:
$terms = get_terms( array(
'taxonomy' => 'testimonial',
'name' => $cat,
'hide_empty' => true,
) );
更新:
根据您的原始问题,get_terms
只有 return 关于您搜索的 术语 的信息。
要获取与术语关联的所有 posts,您需要使用 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 的有效参数,请尝试 'name':
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' );
我试图通过类别名称限制检索到的推荐(使用 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 reference documentation here 列出可接受的参数。你想要的是:
'name':(字符串|数组)可选。 return 个术语的名称或名称数组。
因此,假设 $cat 是您要搜索的名称,请尝试将您的 get_terms 参数更改为:
$terms = get_terms( array(
'taxonomy' => 'testimonial',
'name' => $cat,
'hide_empty' => true,
) );
更新:
根据您的原始问题,get_terms
只有 return 关于您搜索的 术语 的信息。
要获取与术语关联的所有 posts,您需要使用 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 的有效参数,请尝试 'name':
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' );