0

The code below is the 2nd of 4 loops in "page-balls.php". Everything else is working correctly.

I'm trying to display a tennis ball graphic if the post being retrieved has a the term "tennis-ball" term. It's not working. Is this because the PAGE does not have the term TENNIS-BALL?

How can I make this work correctly?

<?php       
$args = array (
'post_type' => 'balls',
'orderby' => 'menu_order',
'order' => 'ASC'                
) ;

$myQuery = new WP_Query( $args ) ;
?>

<?php if ($myQuery->have_posts()) : while ($myQuery->have_posts()) : $myQuery->the_post(); ?>

<?php if(is_tax( 'ball-types', 'tennis-ball' )) : ?> 
    SHOW THE TENNIS BALL GRAPHIC
<?php else : ?>
    SHOW THE PLACEHOLDER GRAPHIC
<?php endif ; ?>

<?php endif ; ?>

<?php wp_reset_postdata() ; ?>

1 Answer 1

3

The codex on is_tax:

This Conditional Tag checks if a custom taxonomy archive page is being displayed.

That is not the condition you want to check for.

Take a look at the has_term function and use it like so:

if( has_term( 'tennis-ball', 'ball-types', $post->ID ) ) { 
    /* SHOW THE TENNIS BALL GRAPHIC */
} else {
    /* SHOW THE PLACEHOLDER GRAPHIC */
}

And as an aside, remove some of the <?php opening and closing tags. They serve no purpose and simply make your code less legible.

2
  • Thanks for the help Johanne. Appreciated. Re. the aside... Actually, I prefer writing my PHP this way. Once there's a lot of HTML in there, I find it much easier to read by placing the PHP in individual blocks. It's just a personal pref.
    – Wicky
    Apr 30, 2013 at 16:38
  • Obviously, @Wicky, you may write your code as you see fit - who am I to say what works for you personally. Should you ever have to work in cooperation with other developers though, I promise you this practice will become an issue. Also, ideally logic and markup don't mix. I realize that's not always possible, though. Anyhoo, if the main aspect of this answer solved your immediate problem (which I am quite confident it did), be so kind as to tick the checkmark to the left of the answer, such that others that have a similar issue can tell it is resolved. Apr 30, 2013 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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