如何为Gravatar头像添加ALT属性

下面由WordPress教程栏目给大家介绍为gravatar头像添加alt属性的方法,希望对需要的朋友有所帮助!

如何为Gravatar头像添加ALT属性

图片ALT属性不仅有利于搜索引擎索引图片,而且当图片无法加载的时候,会显示图片的ALT信息。

wordpress教程文章插入图片时可以在“替代文本”中填写ALT信息,但评论中的大量Gravatar头像一般主题都没有ALT属性,其实WP以为我们预设了Gravatar头像ALT属性参数。

查看WP官网 Codex  get avatar  默认的可选参数:

<?php  echo get_avatar( $id_or_email, $size, $default, $alt, $args ); ?>

其中:$alt 就是 alt可选参数

打开主题评论模板,找到类似这句:

<?php  echo get_avatar( $comment, 64 ); ?>

替换为:

<?php  echo get_avatar( $comment, 64, &#39;&#39;, get_comment_author() ); ?>

将评论者名称作为ALT属性。

如果你的主题调用评论模模块使用的函数是:

wp_list_comments();

暂时在官网上还没找到用该函数添加ALT属性的参数(貌似WordPress默认主题ALT也是空的),只能按下面的代码拆分这个函数,然后修改。

function mytheme_comment($comment, $args, $depth) {     if ( 'div' === $args['style'] ) {         $tag       = 'div';         $add_below = 'comment';     } else {         $tag       = 'li';         $add_below = 'div-comment';     }     ?&gt;      <?php  comment_class( emptyempty( $args[&#39;has_children&#39;] ) ? &#39;&#39; : &#39;parent&#39; ) ?> id="comment-<?php  comment_ID() ?>"&gt;     <?php  if ( &#39;div&#39; != $args[&#39;style&#39;] ) : ?>         <div>" class="comment-body"&gt;     <?php  endif; ?>     <div>         <?php  if ( $args[&#39;avatar_size&#39;] != 0 ) echo get_avatar( $comment, $args[&#39;avatar_size&#39;] ); ?>         <?php  printf( __( &#39;<cite class="fn">%s <span>says:</span>' ), get_comment_author_link() ); ?&gt;     </div>     <?php  if ( $comment->comment_approved == '0' ) : ?&gt;          <em><?php  _e( &#39;Your comment is awaiting moderation.&#39; ); ?></em>           <br>     <?php  endif; ?>     <div> <a>comment_ID ) ); ?&gt;"&gt;         <?php         /* translators: 1: date, 2: time */         printf( __(&#39;%1$s at %2$s&#39;), get_comment_date(),  get_comment_time() ); ?></a><?php  edit_comment_link( __( &#39;(Edit)&#39; ), &#39;  &#39;, &#39;&#39; );         ?>     </div>     <?php  comment_text(); ?>     <div>         <?php  comment_reply_link( array_merge( $args, array( &#39;add_below&#39; => $add_below, 'depth' =&gt; $depth, 'max_depth' =&gt; $args['max_depth'] ) ) ); ?&gt;     </div>     <?php  if ( &#39;div&#39; != $args[&#39;style&#39;] ) : ?>     </div>     <?php  endif; ?>     <?php     }

如果你的主题添加修改了默认的头像调用方式,比如使用CN或者ssl方式调用,该方法将无效。

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享