Add Post Status to Body Class

I have added a custom post status of rejected, working just fine.

I want to add the post status to the body of the post when it is being previewed. In order to style a front end information box seen by the admin.

Topic post-status css Wordpress

Category Web


This is a little late to the party, but for those who might be interested, this can be accomplished without custom code. There is a plugin in the WordPress repository called WP247 Body Classes. You can use the Custom Classes section to add your own custom body classes. In this case, simply adding the following line of code will give you a body class called is-poststat-? where ? will be the post status.

$classes[] = 'is-poststat-' . get_post_status();

Then you can tailor your pages using CSS and checking for body.is-poststat-publish or body.is-poststat-rejected.

Hope this helps someone.


You'll have to use the body_class hook in the functions.php file of your theme in combination with get_post_status() function. Like the following:

add_filter( 'body_class', 'custom_class' );
function custom_class( $classes ) {
    if ( get_post_status() == 'rejected' ) {
        $classes[] = 'rejected';
    }
    return $classes;
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.