I need to add a different class for last post of a Custom Post Type. Is it possible?
I have found this code, but it works only in posts
function wpc_last_post_class( $classes ) {
global $wp_query;
if(($wp_query->current_post+1) == $wp_query->post_count) $classes[] = 'last'; // change the class name you would like to add here
return $classes;
}
add_filter('post_class', 'wpc_last_post_class');
I need to add a different class for last post of a Custom Post Type. Is it possible?
I have found this code, but it works only in posts
function wpc_last_post_class( $classes ) {
global $wp_query;
if(($wp_query->current_post+1) == $wp_query->post_count) $classes[] = 'last'; // change the class name you would like to add here
return $classes;
}
add_filter('post_class', 'wpc_last_post_class');
Share
Improve this question
edited May 24, 2019 at 20:12
nmr
4,5672 gold badges17 silver badges25 bronze badges
asked May 24, 2019 at 19:04
Laura P. NúñezLaura P. Núñez
111 bronze badge
3
|
1 Answer
Reset to default 1Not exactly what you asked for, but given that classes are primarily used for styling, might you consider using CSS pseudo-class ":last-child" instead of PHP code?
Here's some information on how to style using :last-child
https://css-tricks/almanac/selectors/l/last-child/
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745455698a4628476.html
post_class()
function in the template file responsible for displaying the custom post type? – nmr Commented May 24, 2019 at 20:40post_class()
in your template, or you're not using the main query. – Jacob Peattie Commented May 25, 2019 at 5:52