Display related terms from a taxonomy in an entry

I have a function to obtain specific taxonomies in each entry. The problem is, for each entry, it shows all the values t

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
Add a comment  | 

2 Answers 2

Reset to default 0

Please 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>';
        }
    }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744770572a4592733.html

相关推荐

  • Display related terms from a taxonomy in an entry

    I have a function to obtain specific taxonomies in each entry. The problem is, for each entry, it shows all the values t

    5天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信