admin管理员组文章数量:1391860
I have a function to obtain specific taxonomies in each entry. The problem is, for each entry, it shows all the values that the taxonomy contains. How can I do to show the correct value to each entry? I don't know about programming. I appreciate you being clear in the answer. Thank you
$terms = get_terms( ( array('taxonomy' => 'dormitorio', 'hide_empty' => false,)));
if ( isset( $terms ) && '' !== $terms ) {
foreach( $terms as $term ) {
echo '<div class="tax dormitorio ' . $term->slug . '">';
echo $term->name;
echo '</div>';
}
I have a function to obtain specific taxonomies in each entry. The problem is, for each entry, it shows all the values that the taxonomy contains. How can I do to show the correct value to each entry? I don't know about programming. I appreciate you being clear in the answer. Thank you
$terms = get_terms( ( array('taxonomy' => 'dormitorio', 'hide_empty' => false,)));
if ( isset( $terms ) && '' !== $terms ) {
foreach( $terms as $term ) {
echo '<div class="tax dormitorio ' . $term->slug . '">';
echo $term->name;
echo '</div>';
}
Share
Improve this question
asked Feb 6, 2020 at 8:41
MaciMaci
11 bronze badge
1
- I have it inside functions.php How can I use get_the_terms()? I changed it and I 've got differents error. – Maci Commented Feb 6, 2020 at 10:52
2 Answers
Reset to default 0Please specify where are you using the code above, if in a template file or in a function.
Assuming the code above is for a single post and that you're trying to retrieve the dormitorio
terms associated with that post you should use get_the_terms()
instead of get_terms()
.
get_terms() Retrieves the terms in a given taxonomy or list of taxonomies
whilst
get_the_terms() Retrieves the terms of the taxonomy that are attached to the post
I've found the solution:
$terms = get_the_terms( get_the_ID(), 'dormitorio' );
if ( isset( $terms ) && '' !== $terms ) {
foreach( $terms as $term ) {
echo '<div class="tax dormitorio ' . $term->slug . '">';
echo $term->name;
echo '</div>';
}
}
本文标签: Display related terms from a taxonomy in an entry
版权声明:本文标题:Display related terms from a taxonomy in an entry 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744770571a2624313.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论