WordPress.org

Ready to get started?Download WordPress

Forums

PHP Conditionals (3 posts)

  1. Michael Benson
    Member
    Posted 8 years ago #

    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.

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 8 years ago #

    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 http://www.example.com/category/whateverCategory8sNameIs .

    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.

  3. Michael Benson
    Member
    Posted 8 years ago #

    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. :)

Topic Closed

This topic has been closed to new replies.

About this Topic