-1

How can I add classes on the echo returns?

    <div class="author_sidebar">
    <?php if ( is_singular( 'post' ) ) {
echo get_avatar( get_the_author_meta( 'user_email' ), 75 );
echo get_the_author_meta( 'display_name' );
    ?>
    </div>

I managed to solve the problem by rapping the echo returns in divs this then allowed me to add classes into the divs

  • Which text do you want to change and into what? Your code simply displays an image and the user's name. Ps. Note that you're missing a closing } there. – swissspidy Jan 22 '18 at 14:20
  • So I want the users name to have a tag on the HTML so I can style it easier :') – Jordan Kellett Jan 22 '18 at 14:33
  • You mean add an attribute, like a CSS class. There isn't a generic way to do that. You'd need to refer to the documentation for each function to see if there's a way to do it. The simplest way would be to just wrap the output in a div with a class on it. – Jacob Peattie Jan 22 '18 at 14:43
3

If you want to wrap an HTML tag around the user display names, you can simply close and open the PHP tags:

<div class="author_sidebar">
  <?php if ( is_singular( 'post' ) ) { ?>
    <div class="foo"><?php echo get_avatar( get_the_author_meta( 'user_email' ), 75 ); ?></div>
    <div class="bar"><?php echo get_the_author_meta( 'display_name' ); ?>
  <?php } ?>
</div>
3

If you want to wrap the html echoed by get_avatar or get_the_author_meta @swissspidy is totally right.

If you want to add some css class to <img> tag echoed by get_avatar, you can add it by passing it as an argument (as explained in the documentation: https://codex.wordpress.org/Function_Reference/get_avatar).

$args = ['class' => 'foo'];     
echo get_avatar( get_the_author_meta( 'user_email' ), 75, null, null, $args )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.