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

本文标签: Display related terms from a taxonomy in an entry