WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] in_category() question (5 posts)

  1. ubersoft
    Member
    Posted 8 years ago #

    the WordPress Codex says that in_category() is used to query if a post belongs to a specific category. It doesn't seem to allow multiple categories (i.e., you don't seem to be able to use it to set up an if statement for categories 1,2, and 3) so I've been trying to find ways to work around that.

    For example, I want a certain set of navigation tools to appear when posts in categories 2, 3, and 4 are being viewed, but NOT when posts in any other category are being viewed.

    Right now I'm doing this:


    <?php if (in_category(2)) : ?>

    (stuff)

    <?php endif; ?>

    <? php if (in_category(3)) : ?>

    (stuff)

    <?php endif; ?>

    <? php if (in_category(4)) : ?>

    (stuff)

    <?php endif; ?>

    (everything else on the page)

    This is, needless to say, a little cumbersome. I was wondering if I could create a "master category" for categories 2, 3, 4 -- for the purpose of this post, let's call it category 5 -- and use that for all of them. What I mean is, if 2, 3, and 4 are sub-categories of category 5, can I do this:

    <?php if(in_category(5)) : ?>

    (stuff)

    <?php endif; ?>

    and do the same thing as my first example?

  2. moshu
    Member
    Posted 8 years ago #

    I don't know if it would work with a parent cat... (I suspect it wouldn't) but you may try something like:

    <?php if ( in_category(3) || in_category(x) ) { ?>
    do stuff
    <?php } ?>

    (I am also wondering if your code works at all)

  3. ubersoft
    Member
    Posted 8 years ago #

    It works (I was testing it last night and it worked fine) but I decided not to use it because it's a pain to modify all three of them when I want to make a change.

    I'm using WordPress to publish my webcomics -- one that is published M-F, one occasionally, and one very rarely. All three have their own archives, and since I also post site news on the site I want to be able to keep browsing through all that content completely separate, and give the webcomics more sophisticated navigation tools than the news posts have.

    So here are the categories:

    1 - News
    2 - Help Desk
    3 - Kernel Panic (not yet moved over to the site, so at the moment this is just a dummy category)
    4 - Old Skool Webcomic

    At the moment I'm doing this:

    <?php if ( in_category(1)) : ?>

    (news-specific navigation tools)

    <?php else : ?>

    (webcomic-specific navigation tools)

    <?php endif; ?>

    The thing is, though, I can't guarantee that I won't have other post categories in the future, so I really need to make the webcomic navigation tools the focus of the code. The way I'm doing it now, WordPress assumes that anything that is not in category 1 should get the webcomic-specific navigation tools, and if the content of the site should expand in the future that may not be a good assumption to make.

  4. moshu
    Member
    Posted 8 years ago #

    If I understand well what you say, you could use the code I posted above - adding an else...

    <?php if ( in_category(2) || in_category(3) || in_category(4) ) { ?>
    do stuff...;
    } else {
    do some other stuff;
    <?php } ?>

  5. ubersoft
    Member
    Posted 8 years ago #

    Ah... OK, I see what you mean! Thanks! :)

Topic Closed

This topic has been closed to new replies.

About this Topic