Add aditional class to get_avatar when showing image

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”.

Leave a Comment