• Good morning,

    I have 6 categories that will each contain many subcategories. I want to display a different header image depending on what top level category the visitor is under. I know there is a way to specify a conditional statement depending on the category ID but how could I do this to find the category ID of the top level category being visited, regardless if they are in a subcat or not? Thanks for any help!

Viewing 9 replies - 1 through 9 (of 9 total)
  • Under your dashboard, go to Manage —> Categories —> and you’ll see a list of all your cats in this type of format:
    — parent cat
    — — child cat
    — — child cat
    — — child cat
    — parent cat
    — — child cat

    On the far left, right before the name of each category, is the ID number, which is what you put between the ( ) in the conditional statement. If you create a conditional statement for any parent cat, all the child cats under it will also be included unless you specifically designate otherwise.

    Thread Starter liquilife

    (@liquilife)

    Awesome. I’ve already got the conditionals worked out for all the top level cats. Then it appears my setup will work fine just as it is.

    Thanks for the reply 🙂

    If you create a conditional statement for any parent cat, all the child cats under it will also be included unless you specifically designate otherwise.

    That is not correct. is_category() only evaluates the current category. With that conditional you’d need to use an if statement like:

    if( is_category(Parent) || is_category(Child1) || is_category(Child2) || is_category(Child3) || etc… )

    I wrote up code a while back that provides a conditional that returns true for a category ID *or* its child categories. Let me know if you could use that and I’ll wrap it in a plugin.

    Kafkaesqui: I could use that as a plugin!

    Well that’s one yes vote!

    is_category_parent() plugin:
    download plugin | view source

    Download the file and upload it to your plugins directory, then activate is_category_parent() under Plugins. Use it like you would the is_category() conditional:

    <?php if(is_category_parent(10)) : ?>
    ~ Displays if current or parent category is 10 ~
    <?php endif; ?>

    Notes: is_category_parent() only accepts a numeric category ID; to accept category names would require querying the database to match against them, which I felt was overkill. It also only affirms ‘direct’ child or sub- categories. It could be rewritten to work down through any level of category nesting, but again we’re talking about queries to the database…

    Cool. Thanks!

    Thread Starter liquilife

    (@liquilife)

    Awesome, I’m probably going to give this a go. Will this work in the post pages as well? My conditionals I found out only work while viewing category listings.

    liquilife, is_category() only works on category queries, so this only works then, as well. I don’t know if it will help, but you can take a look at two plugins I’ve written that deal with matching category template to post, and in the latter case child category to parent category template:

    http://guff.szub.net/2005/07/21/post-templates-by-category/
    http://wordpress.org/support/topic/58382#post-315039

    It seems that parent categories have category_parent = 0 in the database, so you could use this to filter only parent categories:

    <?php if(is_category_parent('0')) : ?>
    ~ Displays only if current is a parent category ~
    <?php endif; ?>

    This way we can use a template to parent and another one to children categories.

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Need help with a conditional tag and categories’ is closed to new replies.