Select Page

Today I found myself in need of a logged-out body class on a WordPress site I’m building. Anyone who has ever coded a WordPress theme has most likely added the body_class() function to their project and it would look something like the following:

<body <?php body_class(); ?>>

Now the body_class() function allows you to easily add classes directly into the function like the following:

<body <?php body_class('class-name');?> >

Obviously, the problem arises when you want to conditionally include a class name. I wanted to add a class that identifies a user as not logged into the site. For this I used the following code in my functions.php file.

function my_class_names($classes) {
    if (! ( is_user_logged_in() ) ) {
        $classes[] = 'logged-out';
    return $classes;

Obviously, using this logic it will add the “Logged-out” class to the body tag on every page/post/custom taxonomy page across my website. You can include other conditional functions to only show the the class name on certain types of pages such as if a post is in a certain category.

Pin It on Pinterest


Share This

Share this post with your friends!