WordPress.org

Ready to get started?Download WordPress

Forums

Conditional statements in header (3 posts)

  1. danbranigan
    Member
    Posted 5 years ago #

    Hi,

    I'm trying to put a conditional statement in my header that checks to see if the current page is within a certain category and then assigns a class="current" to a link in my menu.

    The menu is a static menu - i.e. not dynamically generated within a loop - so I need to call the in_category() tag from outside the loop.

    Here's where I've got to - I've only done one example
    <li> - but it's not working:

    <li><a
            <?php
     		if ( have_posts() ) { the_post(); rewind_posts(); }
    		if ( in_category(11) ){echo 'class="current';}
    		?>
            href="about/accountants-for-life/">About Us</a></li>
            <li><a href="business-building/business-advisory-services/">Business Building</a></li>
            <li><a href="true-tax-efficiency/tax-advisory-services/">True Tax Efficiency</a></li>
            <li><a href="fixed-fee-accounting/">Fixed Fee Accounting</a></li>
            <li><a href="news-views/">News & Views</a></li>
            <li><a href="client-resources/">Client Resources</a></li>

    Perhaps I'm just going about this all wrong? I'd REALLY appreciate any help with this.

    Dan

  2. danbranigan
    Member
    Posted 5 years ago #

    Just spotted a PHP formatting error - it should be

    if ( in_category(11) ){echo 'class="current"';}

    Alas this didn't make it work though :(

    Any one out there have any ideas?

    Dan

  3. Dagon Design
    Member
    Posted 5 years ago #

    You can use in_category outside the loop, but only if it is a single post request (in other words, it may not work while viewing archives by category, etc..) but that should work for you since it does not need to be highlighted unless that page is being viewed anyway.

    You should not need that loop code you have in there. I believe that it is messing up the results of in_category.

    Just try this:

    <li><a
            <?php if (in_category(11)){echo ' class="current" ';} ?>
            href="about/accountants-for-life/">About Us</a>
    </li>

Topic Closed

This topic has been closed to new replies.

About this Topic