WordPress.org

Ready to get started?Download WordPress

Forums

How to check for specific category in single.php? (5 posts)

  1. ozonshak
    Member
    Posted 5 years ago #

    Hello. I'm trying to perform a check for a specific category in the single.php page for a single post. I can't seem to get it to pick up the category properly. I'm attempting to do this with the following code:

    <?php if (is_category('6') ): ?>
    This is a test
    <?php endif; ?>

    I've also tried swapping in the category name instead of the number. No matter what values I use, my text in the IF statement does not appear on the screen. I see some code on that page for displaying the category ("<?php the_category('') ?>"), but that's all it does. I need to actually use this in an "if" statement. Any ideas? Thanks!

  2. moshu
    Member
    Posted 5 years ago #

    You need to read the documentation before using Conditional_Tags.

  3. ozonshak
    Member
    Posted 5 years ago #

    Thank you for the information on the conditional tags. If I'm understanding this correctly, I probably can't use the "is_category" method on the single.php page because I'm not actually on a category page, correct? If so, I'm still not clear how to solve my problem here. Can you give me any advice on how to do this? Thank you.

  4. moshu
    Member
    Posted 5 years ago #

    My advice is: read more... EVERYTHING on a page that is given to you as a reference.

    in_category('5')
    Returns true if the current post is in the specified category id. read more

    Note: Be sure to check your spelling when testing, "is" and "in" are a big difference.

  5. ozonshak
    Member
    Posted 5 years ago #

    Thanks! I think the advice should be read more carefully :-) I didn't even see that small character difference on the first pass. I'll give this a try. Thanks again.

Topic Closed

This topic has been closed to new replies.

About this Topic