图片ALT属性不仅有利于搜索引擎索引图片,而且当图片无法加载的时候,会显示图片的ALT信息。WordPress文章插入图片时可以在“替代文本”中填写ALT信息,但评论中的大量Gravatar头像一般主题都没有ALT属性,其实WP以为我们预设了Gravatar头像ALT属性参数。
查看WP官网 Codex get avatar 默认的可选参数:
- <?php get_avatar( , , , , ); ?>
其中:就是
打开主题评论模板,找到类似这句:
- <?php get_avatar( , 64 ); ?>
替换为:
- <?php get_avatar( , 64, , get_comment_author() ); ?>
将评论者名称作为ALT属性。
如果你的主题调用评论模模块使用的函数是:
- wp_list_comments();
暂时在官网上还没找到用该函数添加ALT属性的参数(貌似WordPress默认主题ALT也是空的),只能按下面的代码拆分这个函数,然后修改。
展开代码
- mytheme_comment(, , ) {
- ( \’div\’ === [\’style\’] ) {
- = \’div\’;
- = \’comment\’;
- } {
- = \’li\’;
- = \’div-comment\’;
- }
- ?>
- <<?php ?> <?php comment_class( ( [\’has_children\’] ) ? : \’parent\’ ) ?> id=>
- <?php ( \’div\’ != [\’style\’] ) : ?>
- <div id= =>
- <?php ; ?>
- <div =>
- <?php ( [\’avatar_size\’] != 0 ) get_avatar( , [\’avatar_size\’] ); ?>
- <?php printf( __( \'<cite =>%s</cite> <span =>says:</span>\’ ), get_comment_author_link() ); ?>
- </div>
- <?php ( ->comment_approved == \’0\’ ) : ?>
- <em =><?php _e( \’Your comment is awaiting moderation.\’ ); ?></em>
- <br />
- <?php ; ?>
- <div =><a href=>
- <?php
- printf( __(\’%1 at %2\’), get_comment_date(), get_comment_time() ); ?></a><?php edit_comment_link( __( \'(Edit)\’ ), \’ \’, );
- ?>
- </div>
- <?php comment_text(); ?>
- <div =>
- <?php comment_reply_link( ( , ( \’add_below\’ => , \’depth\’ => , \’max_depth\’ => [\’max_depth\’] ) ) ); ?>
- </div>
- <?php ( \’div\’ != [\’style\’] ) : ?>
- </div>
- <?php ; ?>
- <?php
- }
如果你的主题添加修改了默认的头像调用方式,比如使用CN或者SSl方式调用,该方法将无效。
参考:
https://codex.wordpress.org/Function_Reference/wp_list_comments
https://codex.wordpress.org/Function_Reference/get_avatar
https://codex.wordpress.org/Plugin_API/Filter_Reference/get_avatar
暂无评论内容