• 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

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter danbranigan

    (@danbranigan)

    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

    dagon-design

    (@dagon-design)

    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>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Conditional statements in header’ is closed to new replies.