Support » Plugins » Hacks » proofread my stupid conditional statement?

  • Resolved yaydesign


    So the good news is that my sad little attempt at a conditional statement doesn’t crash my site. The bad news is that it also doesn’t work.

    I’ve got two custom taxonomies, “status” and “current league”. Every post has a status, but only some have current leagues. I am trying to display these two taxonomies with the post meta, but I don’t want “League:” to show if there is no current league. The simple part of this (the “status” part) works fine, but the next part does nothing. Could someone kindly suggest a modification that might make it work?

    <?php echo get_the_term_list( $post->ID, 'status', 'Status: ', ', ', '' ); ?><br>
    <?php if (has_term( 'current-league' ) ){
    echo get_the_term_list( $post->ID, 'current-league', 'League: ', ', ', '' );
    } ?>

    Many thanks…

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz


    You are using has_term() wrong. It is for checking if any particular terms are assigned, not just any terms. For your purposes, you would need to supply an array of all terms. This is possible by using get_terms(), but this strikes me as very clunky.

    You could use this:
    <?php if ( get_the_terms( $post->ID, 'current-league')) {
    Still slightly clunky, but not a big deal unless your posts have a huge number of terms assigned. Clunky only because you are getting terms twice, once unformatted, then again formatted.

    Maybe even better is assign the return from get_the_term_list() to a variable and only echo it and “League:” if there are terms involved. I’m not sure what the function returns when there are no terms, but you can easily determine that by using var_dump() on the variable.

    1. Your get_the_terms suggestion works perfectly, and I don’t mind the slight clunkiness. (On the contrary, this way it will match all the rest of my coding.)
    2. You are a good person for helping random people who probably don’t always express their gratitude for your willingness to help. Cheers to you.
    Moderator bcworkz


    Matching coding 🙂 You are too funny!

    I don’t always get thanks, but I also sometimes fail to acknowledge offered thanks in my haste. It all works out I suppose. While I don’t need expressed gratitude, it’s always nice to hear. I’m glad you found a solution.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘proofread my stupid conditional statement?’ is closed to new replies.