add a class when login

i add this code in index.php.
<div class="Div1"></div>. then write some css(absolution position) to the Div1. so when i login in my wordpress. the admin nav will push down some spaces to the site body. so the conetent shows unnormal. now, i want to add a class to <div class="Div1"> when logins. is there a way to get this.

Related posts

Leave a Reply

2 comments

  1. If you are using the body_class() function, a class admin-bar will be added automatically when the admin bar is active. For example:

    <body class="home blog logged-in admin-bar">
    

    You can then use that class in your CSS to specify specific rules:

    .admin-bar .Div1 { /* custom styles */ }
    
  2. You can use jquery for this with a

    <?php if ( is_user_logged_in() ) : ?>
    

    And then use jquery to add class to div

    jQuery(document).ready(function(){
    jQuery(".Div1").addClass("logged-in");
    

    So you would use this

    <?php 
        if ( is_user_logged_in() ) {
            echo '<script type="text/javascript">
                jQuery(document).ready(function(){
                jQuery(".Div1").addClass("logged-in");
                '
        else
            echo '';
    ?>
    

    Paste this in your functions.php file in the header.php or a plugin. Your choice. If the add class does not work try finding the div id that div1 class is inside of and use this instead ‘jQuery(“#divclassparent .Div1”).addClass(“logged-in”);’