• I am trying to code my category.php page so each category (or related categories) displays different custom subnavigation. My code follows:

    <?php if (is_category(‘locations’)) {
    echo “
    <div id=’subnavigation’ class=’nine’>

    </div>”;

    } elseif (is_category(‘menu’||’pizza’||’appetizers’||’soups-salads’||’burgers’||’sandwiches-wraps’||’chicken’||’pasta’||’calzones’||’rocktails’||’microbrews’)) {
    echo”
    <div id=’subnavigation’ class=’ten’>

    </div>”;

    } elseif (is_category(‘about-the-rock’)) {
    echo”
    <div id=’subnavigation’ class=’two’>

    </div>”;

    }else {
    echo”<div id=’subnavigation’ class=’two’>

    </div>”;
    }?>

    The problem I’m having is that only the first two subnavigations will display at all. It’s as if the remaining elseif statements are being ignored.

    The staged site can be viewed here: https://www.therockwfp.com/wordpress

    Any suggestions will be appreciated.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    It’s PHP that should be doing that, not anything WP specific. Check your spaces, though, you have echo" and I’m not sure how that would be treated.

    See also: https://us3.php.net/elseif

    You can have more than two elseif’s. Try this format instead (use of an is_category array and no php echo needed):

    <?php if (is_category('a')) { ?>
    <!-- code here -->
    
    <?php } elseif (is_category(array('b','c'))) { ?>
    <!-- code here -->
    
    <?php } elseif (is_category('d')) { ?>
    <!-- code here -->
    
    <?php } else { ?>
    <!-- code here -->
    
    <?php } ?>
    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    Yeah, the main problem is that this:
    is_category('menu'||'pizza'||'appetizers'||'soups-salads'||'burgers'||'sandwiches-wraps'||'chicken'||'pasta'||'calzones'||'rocktails'||'microbrews')

    is the wrong way to do it. The end result is that that is equivalent to just is_category().

    Thread Starter Arutai

    (@arutai)

    I added the space and combed through the code. Now only the first subnavigation works.

    Thread Starter Arutai

    (@arutai)

    Otto42,

    That code works. The menu is one the of two subnavs that showed. What exactly do you see wrong with it?

    Thread Starter Arutai

    (@arutai)

    iridiax,

    You are golden. Thank you.

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    That code works. The menu is one the of two subnavs that showed. What exactly do you see wrong with it?

    It works, but it doesn’t do what you think it does. It doesn’t return true only on those slugs, it will return true on any category slug. That final elseif can never get executed on a category archive page because that wrong is_category always returns true on a category page.

    As iridiax posted, the correct way to do multiple categories is like this:
    is_category(array('b','c'))

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘elseif statements limited to two?’ is closed to new replies.