The Support Forums will be in read-only mode for a scheduled maintenance window on 01 September 2016 14:00 UTC - 20:00 UTC. More information.

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

  1. Stef Verbeeck
    Posted 4 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
    Posted 4 years ago #

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


  3. Michael
    Forum Moderator
    Posted 4 years ago #

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


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


  4. Stef Verbeeck
    Posted 4 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