search ]

Add Class to the Last Post in the Loop

This function adds a class to the last post in the WordPress loop. Using it lets you style the last post differently on the homepage or archive pages.


function last_post_class($classes){
	global $wp_query;
    
	if(($wp_query->current_post+1) == $wp_query->post_count) $classes[] = 'last';
	return $classes;
    
}
add_filter('post_class', 'last_post_class');

For more on WordPress template structure, see WordPress Template Hierarchy.

Join the Discussion
0 Comments  ]

Leave a Comment

To add code, use the buttons below. For instance, click the PHP button to insert PHP code within the shortcode. If you notice any typos, please let us know!

Savvy WordPress Development official logo