最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论