• 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: https://www.ads-software.com/support/topic/highlighting-current-post-title-in-get_posts-list?replies=8#post-1130031

    Here’s my code. What’s wrong?

    <ul id="2">
    <?php
        $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(); ?>
                </a>
            </li>
    <?php endforeach; ?>
    </ul>

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

    (@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.

    Thread Starter rodrego

    (@rodrego)

    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

    (@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)

    Thread Starter rodrego

    (@rodrego)

    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

    (@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.

    Anonymous User 10765487

    (@anonymized-10765487)

    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.

    Thread Starter rodrego

    (@rodrego)

    Hi jmlapam,

    I copied it from a working code (at least they say so at the thread): https://www.ads-software.com/support/topic/highlighting-current-post-title-in-get_posts-list?replies=8#post-1130031

    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.