RobBenz
RobBenz

Reputation: 609

Change CSS for logged in users WordPress | X Theme

I am attempting to change some css to display:none; when the user is logged in. It's just a "Sign In / Register" link. ( so when the user is logged on they wont see it)

<span class="hide-this-class-when-logged-in"><a href="#">Sign Up / Register</a></span>

I have copied my _header.php file into my child theme, and replaced

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

with this

<body<?php echo (is_user_logged_in() ? ' class="logged-in"' : ''); ?>>

thinking that this would provide an additional .class for logged in users, so I could then

.class {display:block}
.class .logged-in {display:none}

but the body got all screwed up,( everything got a float left; ) and I am back to the drawing board.

Any input on this topic, or how to use body_class() with x theme would be really appreciated. thanks.

Upvotes: 1

Views: 3502

Answers (3)

Achyuth Kumar1
Achyuth Kumar1

Reputation: 1

try this

//create two variables 
    $user_loggedin_class = "loggedin";
    $user_notlogin_class = "notloggin"; 
    // add css like this
    <style type="text/css" >
        .loggedin{
             display: none;
        }
        .notloggin{
             display: display:block;
        }
    </style>
    // your span tag should like this 
    <span class="<?php echo ( (is_user_logged_in())?$user_loggedin_class:$user_notlogin_class );  ?>"><a href="#">Sign Up / Register</a></span>

Upvotes: -1

Andrew Bacon
Andrew Bacon

Reputation: 546

Use this in your header.php file.

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

This built in function already tracks user status, as well as a number of other useful states like page template, page id, etc.

Example of output:

<body class="home page page-id-403 page-template page-template-page-home logged-in admin-bar no-customize-support custom-background group-blog">

https://codex.wordpress.org/Function_Reference/body_class

Upvotes: 3

RobBenz
RobBenz

Reputation: 609

SOLVED!!!

<body
<?php
if ( is_user_logged_in() ) {
    body_class('logged-in');
} else {
    body_class('logged-out');
}
?>
>

goes in your _header.php file

.logged-in{whatever;}
.logged-out{whatever;} 

Upvotes: 0

Related Questions