Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a loop set up to display projects with two types of taxonomies attached to each. My issue is that within the loop I have set up a function to select specific posts to attach different imagery/styling to them and for some reason these posts are printing every taxonomy, not just the taxonomies specifically related to them. The rest of the posts in the loop are displaying their correct taxonomies. Any clues? Full loop code below:

<?php $loop = new WP_Query( array( 'post_type' => 'projects', 'orderby' => 'menu_order', 'order' => 'ASC', 'showposts' => 19 ) ); ?>
            <?php 
            $c = 0;
            while ( $loop->have_posts() ) : $loop->the_post();
            $c++;
        ?>
        <?php if( $c == 5 || $c == 15 || $c == 25 || $c == 35) : ?>
        <li class="left-feature feature">
            <span class="img">
            <a href="<?php the_permalink(); ?>" title="Learn more about <?php the_title(); ?>" rel="bookmark">
                <?php
                    $rows = get_field('feature_image');
                    if($rows)
                    {
                        foreach($rows as $row)
                        {
                            echo '<img src="' . $row['feature'] . '" alt=" " />';
                        }
                    }
                ?>
            </a>
            </span>
            <h2><?php the_title(); ?></h2>
            <?php
                $terms = get_terms('client');
                $count = count($terms);
                if ( $count > 0 ){
                    echo "<p>Client: ";
                    foreach ( $terms as $term ) {
                    echo "<span>";
                    echo $term->name;
                    echo "</span>";
                }
            echo "</p>";
            }
            ?>
            <?php
                $terms = get_terms('type');
                $count = count($terms);
                if ( $count > 0 ){
                    echo "<p>Type: ";
                    foreach ( $terms as $term ) {
                    echo "<span>";
                    echo $term->name;
                    echo "</span>";
                }
            echo "</p>";
            }
            ?>
        </li>
        <?php elseif( $c == 10 || $c == 20 || $c == 30 || $c == 40) : ?>
        <li class="right-feature feature">
            <span class="img">
            <a href="<?php the_permalink(); ?>" title="Learn more about <?php the_title(); ?>" rel="bookmark">
                <?php
                    $rows = get_field('feature_image');
                    if($rows)
                    {
                        foreach($rows as $row)
                        {
                            echo '<img src="' . $row['feature'] . '" alt=" " />';
                        }
                    }
                ?>
            </a>
            </span>
            <h2><?php the_title(); ?></h2>
            <?php
            $terms = get_terms('client');
            $count = count($terms);
                if ( $count > 0 ){
                    echo "<p>Client: ";
                    foreach ( $terms as $term ) {
                        echo "<span>";
                        echo $term->name;
                        echo "</span>";
                    }
                echo "</p>";
                }
            ?>
            <?php
            $terms = get_terms('type');
            $count = count($terms);
                if ( $count > 0 ){
                    echo "<p>Type: ";
                    foreach ( $terms as $term ) {
                        echo "<span>";
                        echo $term->name;
                        echo "</span>";
                    }
                    echo "</p>";
                }
            ?>
        </li>
        <?php else : ?>
        <li class="regular">
            <span class="img">
            <a href="<?php the_permalink(); ?>" title="Learn more about <?php the_title(); ?>" rel="bookmark">
                <?php
                    $rows = get_field('thumbnail_image');
                    if($rows)
                    {
                        foreach($rows as $row)
                        {
                            echo '<img src="' . $row['thumbnail'] . '" alt=" " />';
                        }
                    }
                ?>
            </a>
            </span>
            <h2><?php the_title(); ?></h2>
            <?php
            $terms = get_the_terms($post->ID, 'client');
            echo '<p>Client: ';
            foreach ($terms as $taxindex => $taxitem) {
                echo "<span>";
                echo $taxitem->name;
                echo "</span>";
            }
            echo '</p>'
            ?>
            <?php
            $terms = get_the_terms($post->ID, 'type');
            echo '<p>Type: ';
            foreach ($terms as $taxindex => $taxitem) {
                echo "<span>";
                echo $taxitem->name;
                echo "</span>";
            }
            echo '</p>'
            ?>
        </li>
    <?php endif; endwhile; wp_reset_query(); ?>
    </ul>
share|improve this question

1 Answer 1

up vote 1 down vote accepted

You are a victim of WordPress’ great naming scheme. You don’t need get_terms(), but get_the_terms(). Note the the? That makes a huge difference.

  • get_terms( $taxonomies, $args = '' ): Retrieve the terms in a given taxonomy or list of taxonomies. All terms.

  • get_the_terms( $id, $taxonomy ): Retrieve the terms of the taxonomy that are attached to the post. Only the terms for that post.

Now try something like this in a loop:

$terms = get_the_terms( get_the_ID(), 'category' );
print '<pre>' 
    . htmlspecialchars( 
        print_r( $terms, TRUE ), 
        ENT_QUOTES, 
        'utf-8', 
        FALSE 
    ) 
    . '</pre>';

Sample result:

Array
(
    [4] => stdClass Object
        (
            [term_id] => 4
            [name] => aciform
            [slug] => aciform
            [term_group] => 0
            [term_taxonomy_id] => 4
            [taxonomy] => category
            [description] => 
            [parent] => 0
            [count] => 2
            [object_id] => 188
            [cat_ID] => 4
            [category_count] => 2
            [category_description] => 
            [cat_name] => aciform
            [category_nicename] => aciform
            [category_parent] => 0
        )

    [10] => stdClass Object
        (
            [term_id] => 10
            [name] => Cat A
            [slug] => cat-a
            [term_group] => 0
            [term_taxonomy_id] => 11
            [taxonomy] => category
            [description] => 
            [parent] => 0
            [count] => 2
            [object_id] => 188
            [cat_ID] => 10
            [category_count] => 2
            [category_description] => 
            [cat_name] => Cat A
            [category_nicename] => cat-a
            [category_parent] => 0
        )
)

Now you can iterate over that array and use esc_html( $term->name ).

share|improve this answer
    
Thanks very much for the quick help - the answer was staring me in the face! –  Mister Cairns Nov 5 '12 at 2:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.