Support » Fixing WordPress » PHP Conditionals

  • Now, i am no PHP coding master. And i am struggling to quite comprehend where abouts i have gone wrong with the following code, which is trying to display certain information when Category ID8 is being displayed, and another set of information whenever any other category is being displayed.

    <?php if (is_category('8')) { ?>

    ### example code ###

    <?php } else { ?>

    ### second example code ###

    <?php } ?>

    Currently only code from the else statement segment is being displayed. Even though posts from Category ID8 are being extracted, they are treated as a post from Category ID2 for example.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator Samuel Wood (Otto)

    (@otto42) Admin

    is_category() doesn’t check what category the *post* is in. It checks to see if you’re displaying a category archive page. In your case, it only returns true if the current page is the page with all the posts from category 8. Like .

    If you want to check whether a post is in category 8 or not (while in The Loop), then you need to use in_category(‘8’) instead.

    Ah, thanks for your reply. I knew it must have been something to do with the actual statement itself. Made your suggested fix and now works as intended. 🙂

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘PHP Conditionals’ is closed to new replies.