skip to Main Content

Using this code on my WP site and need help styling the echo lines of code;

              `<?php 
                if ( is_user_logged_in() ) {
                    $current_user = wp_get_current_user();
                    if ( ($current_user instanceof WP_User) ) {
                        echo '' . esc_html( $current_user->display_name );
                        echo get_avatar( $current_user->ID, 38 );
                    }
                    }
                ?>`

Would like to use CSS classes, but when I try the below code it doesn’t work;

echo '<p class="CSS Style">' . ($current_user->display_name ) '</p>';

Thanks to anyone that can help. Probably pretty simple to most developers.

3

Answers


  1. I try run your code, i think you are missing the ‘ . ‘ before </p>.

    echo '<p class="CSS Style">' . ($current_user->display_name ) . '</p>';

    Login or Signup to reply.
  2. Hey you can also do something like this if you want write a standalone P tag outside the PHP logic.

    <?php 
         if ( is_user_logged_in() ) {
            $current_user = wp_get_current_user();
            if ( ($current_user instanceof WP_User) ) { ?>
    
                <p class="username"><?php echo $current_user->display_name; ?></p>
    
    <?php
            echo get_avatar( $current_user->ID, 38 );
    
            }
         }
    ?>
    

    Otherwise, you can only add a dot before P closing tag.

    Login or Signup to reply.
  3. Use this line of code

    echo '<p class="CSS Style">' . ($current_user->display_name ).'</p>';
    

    Your forgot to give period(.);

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search