css - Is possible add class for last post of Custom Post Type?

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

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
  • Do you use the post_class() function in the template file responsible for displaying the custom post type? – nmr Commented May 24, 2019 at 20:40
  • 1 You could try the accepted method here - stackoverflow/questions/9610985/… – Faye Commented May 24, 2019 at 21:48
  • There's no reason this wouldn't work on a custom post type, unless you're not using post_class() in your template, or you're not using the main query. – Jacob Peattie Commented May 25, 2019 at 5:52
Add a comment  | 

1 Answer 1

Reset to default 1

Not 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

相关推荐

  • css - Is possible add class for last post of Custom Post Type?

    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

    3小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信