get_avatar() (get the avatar)
The get_avatar() function is used to obtain the top mailbox or user's avatar code, and is very commonly used in comment lists.
This function provides a get_avatar filter to filter the Html code (img tag) of the avatar.
If "Show avatar options" is turned off in "Discussion" in the background "Settings" then returns False.
usage
get_avatar( $id_or_email, $size, $default, $alt );
parameter
$id_or_email
(Integer | String | Object) (Required) User ID; Email; Comment or User object. If you can use the get_the_author_meta( 'ID' ) function in the loop to call the author of the current article.
Default value: None
$size
(Integer) (Optional) The size of the avatar is up to 512, and the unit is pixels (px).
Default value: 96
get_avatar() (get the avatar)
$default
(String) (Optional) If there is no image returned by the avatar, the default is "Mysterious Man", which can be the image URL.
Default value: empty string (mysterious person)
$alt
(String) (Optional) The avatar img tag's alt attribute content.
Default value: False
Return value
(String | Boolean) Returns the img tag of an avatar, and returns False if the "Show avatar option" is turned off in the "Discussion" of the background "Settings" backend.
example
Comments list:
<?php echo get_avatar( $GLOBALS['comment'] ); ?>
Loop to get the author's avatar of the current article:
<?php echo get_avatar( get_the_author_meta( 'ID' ), 32 ); ?>
Custom email:
<?php echo get_avatar( 'email@', 32, '', 'Customize email' ); ?>
Custom avatar picture alt tag
WordPress uses Gravatar avatar by default, and is called through the get_avatar() function, which is generally used when calling comments.
Use the fourth property of the get_avatar() function, $alt, to set the alt tag of the return code, but most people will not set it when using it, so it becomes blank.
Search engine crawlers cannot read what content is on the picture, and can only rely on its alt tag. Without the alt tag, it is very bad for SEO.
If you want to add an alt tag to all avatars, you can put the following code into the (learning more) file in the topic.
/** *WordPress custom avatar picture alt tag * Generate different alt tags according to different lives */avatar-alt/ */ function Bing_avatar_alt( $avatar ){ $alt = 'Gravatar avatar';//Alt tag content $before = strpos( $avatar, "alt='" ); $after = strpos( $avatar, "'", $before ); if( $before === false || $after === false ) return $avatar; $alt = esc_attr( $alt ); return substr( $avatar, 0, $before ) . "alt='$alt" . substr( $avatar, $after + strlen( "'" ) ); } add_filter( 'get_avatar', 'Bing_avatar_alt' );
That's it. Pay attention to modifying the $alt variable in the code to the content of the alt tag you want to modify.
Generate different alt tags according to different people
If you want to place different alt tags according to different people, you can use the following example code:
/** *WordPress custom avatar picture alt tag */avatar-alt/ */ function Bing_avatar_alt( $avatar, $id_or_email ){ if( $id_or_email == 'bingoblog@' || $id_or_email === 1 ){ $alt = 'Blogger Gravatar avatar';//Blogger's alt tag content }else{ $alt = 'Gravatar avatar';//Alt tag content for ordinary visitors } $before = strpos( $avatar, "alt='" ); $after = strpos( $avatar, "'", $before ); if( $before === false || $after === false ) return $avatar; $alt = esc_attr( $alt ); return substr( $avatar, 0, $before ) . "alt='$alt" . substr( $avatar, $after + strlen( "'" ) ); } add_filter( 'get_avatar', 'Bing_avatar_alt', 10, 2 );
If the alt tag has been set, it will not be replaced
If you need to use the following code if the content of the alt tag has been set (the $alt attribute of the get_avatar() function) if you do not replace it:
/** *WordPress custom avatar picture alt tag *If the alt tag has been set, it will not be replaced */avatar-alt/ */ function Bing_avatar_alt( $avatar, $id_or_email, $size, $default, $alt ){ if( !empty( $alt ) ) return $avatar; $alt = 'Gravatar avatar';//Alt tag content $before = strpos( $avatar, "alt='" ); $after = strpos( $avatar, "'", $before ); if( $before === false || $after === false ) return $avatar; $alt = esc_attr( $alt ); return substr( $avatar, 0, $before ) . "alt='$alt" . substr( $avatar, $after + strlen( "'" ) ); } add_filter( 'get_avatar', 'Bing_avatar_alt', 10, 5 );