I realised that $wp_query doesn’t work for where I had customised the queries with WP_query
. This is completely obvious I suppose when you know, but I didn’t, so here is the adjusted code in case it benefits some other amateur! Note that $media_query, $events_query etc are the instances of WP_query
which I had created myself on the respective archive pages when I did the custom queries, so you’d need to do your own versions of these using whatever you used for the queries. I say this in case someone thinks these were defined by wordpress and try to copy and paste. ( $wp_query itself on the other hand of course is. )
// Add css class 'last' to last posts in archive query [ used in conjunction with post_class('archive-post') ]
add_filter('post_class', 'last_post_class');
function last_post_class($classes) {
// Regular Archives
global $wp_query;
if(($wp_query->current_post+1) == $wp_query->post_count)
$classes[] = 'last';
// Media Archive
global $media_query;
if(($media_query->current_post+1) == $media_query->post_count)
$classes[] = 'last';
// Events Archive
global $events_query;
if(($events_query->current_post+1) == $events_query->post_count)
$classes[] = 'last';
// Author Archive
global $author_query;
if(($author_query->current_post+1) == $author_query->post_count)
$classes[] = 'last';
return $classes;
}