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>
You are a victim of WordPressâ great naming scheme. You donât need
get_terms()
, butget_the_terms()
. Note thethe
? 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:
Sample result:
Now you can iterate over that array and use
esc_html( $term->name )
.