functions - How to append classname to body tag if guest user

By default when a user is logged in, a 'logged-in' classname is appended to the body tag. I need to append som

By default when a user is logged in, a 'logged-in' classname is appended to the body tag. I need to append something like 'guest-user' to the body to output different styling based on whether or not the user is logged in.

How can I achieve this? Something maybe I could put in my child theme's functions.php file?

Thanks.

By default when a user is logged in, a 'logged-in' classname is appended to the body tag. I need to append something like 'guest-user' to the body to output different styling based on whether or not the user is logged in.

How can I achieve this? Something maybe I could put in my child theme's functions.php file?

Thanks.

Share Improve this question asked Sep 24, 2019 at 14:36 JosephJoseph 31 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 1

Yes, there's the body_class filter. You could use e.g.

function body_class_guest_user( $classes, $class ) {
    if ( ! is_user_logged_in() ) {
        $classes[] = 'guest-user';
    }
    return $classes;
}
add_filter( 'body_class', 'body_class_guest_user', 10, 2 );

However you could equally well just use the :not() selector, e.g. body:not(.logged-in) (browser support), or absence of logged-in i.e. set up the guest-user styling as default but then override it if .logged-in.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745133772a4613101.html

相关推荐

  • functions - How to append classname to body tag if guest user

    By default when a user is logged in, a 'logged-in' classname is appended to the body tag. I need to append som

    11小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信