给WordPress评论中的Gravatar头像图片添加ALT属性

给WordPress评论中的Gravatar头像图片添加ALT属性

今天在bing管理员后台做SEO分析的时候提示网站图片没有alt信息,建议加上,然后看了下,有些文章是特色图片没ALT,更多的是Gravatar头像图片没有ALT信息,所以就来研究了下怎么给评论区的Gravatar头像图片添加ALT信息。

Gravatar头像图片添加ALT属性方法:代码版

这个方法很简单,直接编辑你当前使用主题的函数文件 (functions.php),添加下面这串代码保存就行了。

/**
* WordPress评论区gravatar头像添加alt属性
* 详情介绍:https://blog.naibabiji.com/tutorial/wordpress-gravatar-tou-xiang-jia-alt.html
**/
function gatttf_replace_content( $text ) {

  if ( have_comments() ) {
    $alt = get_comment_author();
  } else {
    $alt = get_the_author_meta( 'display_name' ); 
  }
  $text = str_replace( 'alt=\'\'', 'alt=\'Avatar for '.$alt.'\' title=\'Gravatar for '.$alt.'\'',$text );
  return $text;
}

add_filter( 'get_avatar','gatttf_replace_content' );

Gravatar头像图片添加ALT属性方法:插件版

如果你是WordPress新手,不会添加代码,那么可以用插件版,直接在后台搜索Fix Gravatar Alt Text & Title Tag,然后安装就行了。

最终效果如下:

https://wordpress.org/plugins/fix-gravatar-alt-text-title-tag/

5/5 - (1 vote)
滚动至顶部