• OK, I’m not to great at this stuff and I’ve confused myself a little, I’m working on a navigation menu with an ‘active’ class, so far I have most parts working with code like this:

    <li><a href="/index.php" title="home" <?php if(is_home()) {?>class="current"<?php } ?>><span></span>Home</a></li>

    This works perfectly however I’m trying to create a new active button for any posts in the ‘news’ category using this code…

    <li><a href="/news/" title="News" <?php if (is_page_template('news.php')) {?>class="current"<?php } ?><?php if (in_category('3')) {?>class="current"<?php } ?>><span></span>News</a></li>

    but this also highlights the news tab when I’m on the index page, so what I’d like to do is create some code that tells wordpress…

    if is in category 'news' then class="current" but if is in category 'news' and the current page is homepage class= nothing

    anyone able to help me out?

    Thanks.

Viewing 9 replies - 1 through 9 (of 9 total)
  • hi

    instead of is_home() try using is_front_page() that may solve your issue altogether

    Thread Starter threevisual

    (@threevisual)

    thanks for the reply, it’s the news button that is causing the problem though, it’s showing as active on the homepage so I somehow need to tell it that if it’s the homepage don’t show the class=”current”

    why do you need if (in_category(‘3’)) on the news page in the first place? does the is_page_template(‘news.php’) cover the condition for the news page? if not, please explain why that is not enough, so I can help you figure this out.

    Thread Starter threevisual

    (@threevisual)

    so at the moment there is a news.php (the news landing page with a custom page template) and then separate posts in the news *category*.

    The homepage has content from various categories but for some reason the code i’m using in the header…

    <li><a href="/news/" title="News" <?php if (is_page_template('news.php')) {?>class="current"<?php } ?><?php if (in_category('3')) {?>class="current"<?php } ?>><span></span>News</a></li>

    confuses wordpress and when I’m on the index page it thinks it’s viewing content from the news category so shows the class as current for both the home button and the news button.

    It’s quite hard to explain, so I’m not sure if that helped, but take a look at navigation at https://www.wiredbristol.com/index.php and you’ll see what I mean (hopefully)

    (basically I don’t want the news button to show as active if it’s the index page)

    thanks again.

    Hum…

    Am I right… ? The category “3” is the category “news” he?

    I would guess that that your last post is in the category “news”…

    So, the conditionnal “if in_category” result to “true” when you are on index.php… Because the last post in the loop is indeed in this category…

    Does it makes sense ?

    S.

    Thread Starter threevisual

    (@threevisual)

    Thanks Simon, yes that makes perfect sense, you’re right, the last post is in category ‘3’ just need to try and figure a way around this

    Ok…

    Hum…

    Try something.

    Instead of using index.php for your front page, make a file called “home.php” and copy/paste the content of your index.php in it. Save.

    Do not delete the index.php. Just upload the new home.php with exactly the same content to your theme folder…

    S.

    Ok ok… My trick above will obviously not works… :-))

    Here is the solution:

    From what I can tell, if you need the “in_category” it’s because you want the menu to be hilighted when you see a single post page in the category news.

    Is so, just embed the if in_category condtion in a if is_single…

    <?php
    if (is_single()) {
    if (in_category('9')) { ?>
    Your stuff
    <?php } ?>
    <?php } ?>

    You see the trick ? This condition will return “true” only if you are IN category 3 AND on a single post page….

    Should works…

    S.

    Thread Starter threevisual

    (@threevisual)

    genius! thank you.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘if is in ‘news’ category but is not index’ is closed to new replies.