Add class to current post for styling

Display Posts adds a class of .listing-item to each post in the listing. You can customize the classes using the display_posts_shortcode_post_class filter.

If you’re using Display Posts for a table of contents in a sidebar, it’s helpful to style the current post differently than the others. The code below will add a class of .current-list-item if it’s the current post.

This code snippet goes in a core functionality plugin or Code Snippets.

/**
 * Display Posts - current-list-item class 
 * @see https://displayposts.com/2019/08/20/add-class-to-current-post-for-styling/
 *
 * @param array $classes
 * @param object $post
 * @return array $classes
 */
function be_dps_current_class( $classes, $post ) {
	if( is_singular() && $post->ID === get_queried_object_id() ) {
		$classes[] = 'current-list-item';
	}
	return $classes;
}
add_filter( 'display_posts_shortcode_post_class', 'be_dps_current_class', 10, 2 );

Filters used:

Published by Bill Erickson

Bill Erickson is a freelance WordPress developer and a contributor to the Genesis framework. For the past 14 years he has worked with attorneys, publishers, corporations, and non-profits, building custom websites tailored to their needs and goals.