最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

avatar - Add aditional class to get_avatar when showing image

programmeradmin4浏览0评论

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.

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.

Share Improve this question asked Apr 20, 2016 at 15:27 lonerunnerlonerunner 3451 gold badge6 silver badges16 bronze badges 6
  • Your code works fine for me. Are there any plugins or something using get_avatar filter? – James Vu Commented Apr 20, 2016 at 16:19
  • Nothing that can make a problem to the function, i searched through the whole project and only plugin that uses get_avatar is jetpack plugin but i doubt that this can make a problem. What combination did you tried and works? – lonerunner Commented Apr 20, 2016 at 16:52
  • I copied your first line of code, changed the user id. That's it. Did you try with other themes? – James Vu Commented Apr 20, 2016 at 16:55
  • Did you ever figure this out? – JacobTheDev Commented Jun 5, 2016 at 19:05
  • No @Rev i didn't, now when i have some free time again i continue to work on my theme and stumbled on this problem, i came back here to see for any other answers but nothing. I still can't figure out why it's not accepting the args. – lonerunner Commented Jul 23, 2016 at 14:36
 |  Show 1 more comment

3 Answers 3

Reset to default 6

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

Try this one..

<?php echo get_avatar( get_the_author_meta('ID'), $args['image_size'], '', 'alt', array('class' => 'avatar_class') ); ?>

Example:

<?php echo get_avatar( get_the_author_meta('ID'), $args['96'], '', 'avatar', array('class' => 'myclass') ); ?>

Is there any chance you are using the plugin WP User Avatar. I have just run into this where it seems that it is filtering the get_avatar function but not passing the last $args argument through that would contain the class info.

I'm going to start looking for a different solution to uploading custom avatars...

发布评论

评论列表(0)

  1. 暂无评论