如何為Gravatar頭像添加ALT屬性

下面由WordPress教程欄目給大家介紹為gravatar頭像添加alt屬性的方法,希望對需要的朋友有所幫助!

如何為Gravatar頭像添加ALT屬性

圖片ALT屬性不僅有利于搜索引擎索引圖片,而且當圖片無法加載的時候,會顯示圖片的ALT信息。

wordpress教程文章插入圖片時可以在“替代文本”中填寫ALT信息,但評論中的大量Gravatar頭像一般主題都沒有ALT屬性,其實WP以為我們預設(shè)了Gravatar頭像ALT屬性參數(shù)。

查看WP官網(wǎng) Codex? get avatar? 默認的可選參數(shù):

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

其中:$alt 就是 alt可選參數(shù)

打開主題評論模板,找到類似這句:

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

替換為:

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

將評論者名稱作為ALT屬性。

如果你的主題調(diào)用評論模模塊使用的函數(shù)是:

wp_list_comments();

暫時在官網(wǎng)上還沒找到用該函數(shù)添加ALT屬性的參數(shù)(貌似WordPress默認主題ALT也是空的),只能按下面的代碼拆分這個函數(shù),然后修改。

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     }

如果你的主題添加修改了默認的頭像調(diào)用方式,比如使用CN或者ssl方式調(diào)用,該方法將無效。

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點贊6 分享