WordPress.org

Forums

[resolved] in_category only works in one category (4 posts)

  1. Stef Verbeeck
    Member
    Posted 3 years ago #

    Hi all,

    On a site we've made, we use 'in_category' to determine if a subnavigation with child categories should be shown as big blue buttons under the main nav.

    The code in category.php looks like this:

    [Code moderated as per the Forum Rules. The maximum number of lines of code that you can post in these forums is ten lines. Please use the pastebin]

    For the first parent ('inhouse'), this works just fine. For the parent in the elseif-statement ('outdoors') however, nothing is shown.

    Any ideas on how to fix this? The category names are correct, yet nothing echoes for the outdoor category. Thanks in advance.

  2. Stef Verbeeck
    Member
    Posted 3 years ago #

    As my code was removed, check pastebin for the category.php snippet.

    http://pastebin.com/Kf2zhpcM

  3. Michael
    Forum Moderator
    Posted 3 years ago #

    in_category() refers to categories of a post, and does not reliably work outside the loop;

    http://codex.wordpress.org/Function_Reference/in_category

    are you possibly trying to use is_category() which checks for category archives?

    http://codex.wordpress.org/Function_Reference/is_category

  4. Stef Verbeeck
    Member
    Posted 3 years ago #

    Thanks alchymyth!

    Your answer got me thinking, which eventually fixed this problem. I used the "is_category" function, however this only works for the parent category. This means my nav disappeared as soon as you would navigate to a child category.

    Using the great technique I found here, this problem was resolved by adding a little snippet to the functions.php file of my theme.

Topic Closed

This topic has been closed to new replies.

About this Topic