Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Unlike the (WP standard) Custom Menu widget, the Recent Posts widget does not add the current_menu_item class when a post is selected (being displayed).

This seems like a major oversight, am I missing something? If not, does anyone know of a third party widget that lists the most recent posts and includes the current_menu_item class (or equivalent)? ...or perhaps some magical hack to achieve the result of adding a class to the active menu item?

share|improve this question
add comment

1 Answer

The core Recent Posts Widget does not output any classes for the current post - probably because the Widget semantically is a list of posts, rather than a navigation menu, and therefore has no intended purpose to reflect current location.

That said, with a bit of CSS/jQuery trickery, you could target the current post by matching the current post's permalink and/or ID to the href and/or title attribute of the Recent Posts Widget list item link.

While the implementation itself is probably outside the scope of WPSE, the relevant code is here:

<?php  while ($r->have_posts()) : $r->the_post(); ?>
    <li><a href="<?php the_permalink() ?>" title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?></a></li>
<?php endwhile; ?>

Use jQuery to match e.g. .widget li a[href=<?php get_permalink(); ?>], or something along those lines.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.