Forums

if is in 'news' category but is not index (10 posts)

  1. ThreeVisual
    Member
    Posted 3 years ago #

    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.

  2. stvwlf
    Member
    Posted 3 years ago #

    hi

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

  3. ThreeVisual
    Member
    Posted 3 years ago #

    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"

  4. stvwlf
    Member
    Posted 3 years ago #

    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.

  5. ThreeVisual
    Member
    Posted 3 years ago #

    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 http://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.

  6. SimonJ
    Member
    Posted 3 years ago #

    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.

  7. ThreeVisual
    Member
    Posted 3 years ago #

    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

  8. SimonJ
    Member
    Posted 3 years ago #

    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.

  9. SimonJ
    Member
    Posted 3 years ago #

    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.

  10. ThreeVisual
    Member
    Posted 3 years ago #

    genius! thank you.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.