Support » Plugins » Hacks » Dynamic highlight not working

  • Hi folks,

    I’m trying to highlight the current post in a dynamic menu but WordPress is beating me on this.

    The problem is the menu is totally dynamic. Items can be added to it. So the WordPress default solution doesn’t work. I used the solution proposed in this post, but it’s not working:

    Here’s my code. What’s wrong?

    <ul id="2">
        $IDOutsideLoop = $post->ID;
        global $post;
        $args = array( 'numberposts' => 500, 'category' => 3 );
        $myposts = get_posts( $args );
        foreach( $myposts as $post ) :	setup_postdata($post); ?>
           <li<?php if($IDOutsideLoop == $post->ID) { echo " class=\"current\""; } ?>>
               <a href="<?php the_permalink(); ?>" >
    		<?php the_title(); ?>
    <?php endforeach; ?>

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator bcworkz


    I’m guessing that for your situation, the current post ID is not contained in $post->ID when the line $IDOutsideLoop = $post->ID; is executed, otherwise this solution should work.

    Assuming of course you have properly set up the CSS for li.current to be highlighted.

    Hi, bcworkz, thanks for your answer.

    If I put the line $IDOutsideLoop = $post->ID; inside the “foreach” command, all
    li tags get the “current” attached to it. If I put it outside, no matter where, then none get it…

    Is there a different way to do it?

    Moderator bcworkz


    For whatever reason, $post does not contain the current post object for your application. You need to find a different way to identify the current post. Maybe $IDOutsideLoop = get_the_ID(); ? (FWIW, blindly using $post is a bit hacky anyway)

    Doesn’t work… It seems like get_the_ID; only works inside the loop, which is not my case?

    Please let me know if you have any more ideas… Kind of stuck with that.

    Moderator bcworkz


    Right, inside loop only. You’ll need to examine the source code to find some sort of post identifying information. It has to exist in some form to get the current post out of the DB.

    Hi Rodrego

    $IDOutsideLoop = $post->ID;
    if($IDOutsideLoop == $post->ID)

    I don’ get it. What do you want to do? This if statement is wrong, useless at best.

    Hi jmlapam,

    I copied it from a working code (at least they say so at the thread):

    Maybe I did something wrong. How can I make it work?

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Dynamic highlight not working’ is closed to new replies.