admin管理员组文章数量:1425701
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/
本文标签: cssIs possible add class for last post of Custom Post Type
版权声明:本文标题:css - Is possible add class for last post of Custom Post Type? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745455697a2659092.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