Forums

[resolved] if conditionals - anyway to use 'but not' (4 posts)

  1. englishlion
    Member
    Posted 1 year ago #

    I've been messing with my theme to make a dynamic menu that suits my needs and it works quite well, I just have one issue left to solve. My menu highlights the section that you are currently in and one of those menu options is a category so I've used the following code:

    <?php if (is_category('5') || in_category('5')) echo('class="current" '); ?>

    this picks up the category page and any post in that category. I also have the same sort of thing for my home page as follows:

    <?php if (is_home()) echo('class="current" '); ?>

    the problem occurs if the latest post is in category 5, because then the home page seems to also be classed as category 5. This results in both the home and cat5 menu items being shown in class="current". Is there anyway of adding a 'but not home' to the if conditional in the cat5 code above?

  2. converting2wp
    Member
    Posted 1 year ago #

    Is this what you mean?

    if ( (is_category('5') || in_category('5')) && !is_home() )

  3. englishlion
    Member
    Posted 1 year ago #

    Thank you

    if ( (is_category('5') || in_category('5') && !is_home() )
    seems to work fine. I'm assuming ! means NOT.

    As you can tell I'm not a fully fledged programmer!

  4. converting2wp
    Member
    Posted 1 year ago #

    Yes "!" is "not" and && is the logical AND.

    Note the new set of parentheses that group your original two conditions, so the flow is

    ( TEST1 OR TEST2) AND TEST3

Topic Closed

This topic has been closed to new replies.

About this Topic