Add Post ID class to each listing item

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.

The following code adds a class to each listing item of post-{id} , so if the post has an ID of 123 it will have a class of post-123.

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

/**
 * Display Posts - post ID class 
 * @see https://displayposts.com/2019/08/29/add-post-id-class-to-each-listing-item/
 *
 * @param array $classes
 * @param object $post
 * @return array $classes
 */
function be_dps_post_id_class( $classes, $post ) {
	$classes[] = 'post-' . get_the_ID();
	return $classes;
}
add_filter( 'display_posts_shortcode_post_class', 'be_dps_post_id_class', 10, 2 );

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.