I want to add bootstrap img-responsive and img-rounded classes to avatar image when displaying one. But for some reason the class is not displayed when using get_avatar.

By WordPress codex there is an attributes list that you can use in get_avatar to alter the function but my doesn’t pickup class array list.

Here is current code that i use.

get_avatar( $current_user->user_email, 128, null, null, array('class' => array('img-responsive', 'img-rounded') ) );

By explanation last parameter is arguments array in which you can use size , height , width etc… among those is class which can be array or string.

So i tried a few combinations

$args = array(
   'class' => 'img-responsive img-rounded'
 );
get_avatar( $current_user->user_email, 128, null, null, $args );

I also tried

$args = array(
 'class' => array( 'img-responsive', 'img-rounded');
);

But for some reason class is not accepted.

3 Answers
3

I had this problem, too. Here’s the solution for version 4.7.3 if anyone comes across this.

get_avatar( $id_or_email = get_the_author_meta( 'user_email' ), $size="60", $default, $alt, $args = array( 'class' => array( 'd-block', 'mx-auto' ) ) );

or shorter version

get_avatar( get_the_author_meta( 'user_email' ), '60', $default, $alt, array( 'class' => array( 'd-block', 'mx-auto' ) ) );

For some reason, all the parameters have to be present or it doesn’t work.

This method, unlike the functions.php method, will not alter get_avatar globally. So you can have different classes like “post-author” or “comments-author”.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *