admin管理员组文章数量:1292568
I have the below code successfully running on my custom post type archive pages. The only issue I have is that I want the list of 'book_cat' taxonomy terms for that post but ONLY if they're also children of the taxononmy term with an id of 5.
Trying to use the post ID as a parameter isn't working. Can anyone tell me what I'm doing wrong please?
$args = array( 'hide_empty' => '0','taxonomy' => 'book_cat', 'child_of' => 5);
$categories = get_terms($args);
if($categories){
echo '<ul class="characters">';
foreach($categories as $category) {
$link = get_term_link($category);
echo '<li>';
$size = "thumbnail";
$image = get_field('main_image', 'category_'.$category->term_id);
echo '<a href="'.$link.'">';
echo wp_get_attachment_image( $image, 'thumbnail', "", ["class" => "character"] );
echo '</a>';
echo '<span class="cat-title"><a href="'.$link.'">' . $category->name . '</a></span>';
echo '</li>';
}
echo '</ul>';
}
I have the below code successfully running on my custom post type archive pages. The only issue I have is that I want the list of 'book_cat' taxonomy terms for that post but ONLY if they're also children of the taxononmy term with an id of 5.
Trying to use the post ID as a parameter isn't working. Can anyone tell me what I'm doing wrong please?
$args = array( 'hide_empty' => '0','taxonomy' => 'book_cat', 'child_of' => 5);
$categories = get_terms($args);
if($categories){
echo '<ul class="characters">';
foreach($categories as $category) {
$link = get_term_link($category);
echo '<li>';
$size = "thumbnail";
$image = get_field('main_image', 'category_'.$category->term_id);
echo '<a href="'.$link.'">';
echo wp_get_attachment_image( $image, 'thumbnail', "", ["class" => "character"] );
echo '</a>';
echo '<span class="cat-title"><a href="'.$link.'">' . $category->name . '</a></span>';
echo '</li>';
}
echo '</ul>';
}
Share
Improve this question
asked May 13, 2021 at 9:17
friendly_llamafriendly_llama
1054 bronze badges
1 Answer
Reset to default 1If I understood right, right now you get all child terms of term with id = 5, but you need to show only terms from this list, which are attached to the post.
get_terms()
Retrieves the terms in a given taxonomy or list of taxonomies.
Try to use wp_get_post_terms()
instead, which allows you to set post id.
Make sure $post->ID is available.
wp_get_post_terms()
Retrieves the terms for a post.
$args = array( 'hide_empty' => '0','taxonomy' => 'book_cat', 'child_of' => 5);
$categories = wp_get_post_terms($post->ID, 'book_cat', $args);
//rest of your code goes here.....
本文标签: custom taxonomyGet terms of a post but only if they39re also the child of a specific term
版权声明:本文标题:custom taxonomy - Get terms of a post but only if they're also the child of a specific term 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741556670a2385198.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论