Forums

[resolved] in_category() stops working correctly once addl get_posts() is added (3 posts)

  1. ZedAgent
    Member
    Posted 2 years ago #

    I have a simple template which inserts a CSS class depending which category a post is in.

    You can see it in action here:

    http://www.fbala.com/category/press/

    You can see the single post on that page. If you click the title of the post to take you to a single post page, the highlighting on the right is incorrect. 'Events' is highlighted when 'Press' should be, as this post is in the press category. The code I'm using to make that distinction is simple as can be:

    <a href="/events/"<?php if (in_category('events')) echo ' class="currentsection"';?>>Events</a>
    <a href="/press/"<?php if (in_category('press')) echo ' class="currentsection"';?>>Press</a>

    This was working as intended up until I implemented the recent post 'newsflash' box at top. Here's the code for that:

    <?php $myposts = get_posts('numberposts=2&cat=4,3');
    foreach($myposts as $post):  ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title();?></a></li>
    <?php endforeach; ?>

    If I remove that code, the 'Press' navigation link highlights as it should on the single post page.

    Any ideas?

    Thanks for your time,

    Adam

  2. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

    Try adding <?php wp_reset_query();?> to the end of that plugin code.

  3. ZedAgent
    Member
    Posted 2 years ago #

    You sir are a scholar and a gentleman. Had a feeling it was something along those lines (I'd tried refreshing the post cache after the query but that didn't do the trick.) Thank you!

    -A

Topic Closed

This topic has been closed to new replies.

About this Topic