下面由WordPress教程欄目給大家介紹為Gravatar頭像添加ALT屬性的方法,希望對(duì)需要的朋友有所幫助!
圖片ALT屬性不僅有利于搜索引擎索引圖片,而且當(dāng)圖片無法加載的時(shí)候,會(huì)顯示圖片的ALT信息。
WordPress文章插入圖片時(shí)可以在“替代文本”中填寫ALT信息,但評(píng)論中的大量Gravatar頭像一般主題都沒有ALT屬性,其實(shí)WP以為我們預(yù)設(shè)了Gravatar頭像ALT屬性參數(shù)。
查看WP官網(wǎng) Codex get avatar 默認(rèn)的可選參數(shù):
<?php echo get_avatar( $id_or_email, $size, $default, $alt, $args ); ?>
其中:$alt 就是 alt可選參數(shù)
打開主題評(píng)論模板,找到類似這句:
<?php echo get_avatar( $comment, 64 ); ?>
替換為:
<?php echo get_avatar( $comment, 64, '', get_comment_author() ); ?>
將評(píng)論者名稱作為ALT屬性。
如果你的主題調(diào)用評(píng)論模模塊使用的函數(shù)是:
wp_list_comments();
暫時(shí)在官網(wǎng)上還沒找到用該函數(shù)添加ALT屬性的參數(shù)(貌似WordPress默認(rèn)主題ALT也是空的),只能按下面的代碼拆分這個(gè)函數(shù),然后修改。
function mytheme_comment($comment, $args, $depth) { if ( 'div' === $args['style'] ) { $tag = 'div'; $add_below = 'comment'; } else { $tag = 'li'; $add_below = 'div-comment'; } ?> <<?php echo $tag ?> <?php comment_class( emptyempty( $args['has_children'] ) ? '' : 'parent' ) ?> id="comment-<?php comment_ID() ?>"> <?php if ( 'div' != $args['style'] ) : ?> <div id="div-comment-<?php comment_ID() ?>" class="comment-body"> <?php endif; ?> <div class="comment-author vcard"> <?php if ( $args['avatar_size'] != 0 ) echo get_avatar( $comment, $args['avatar_size'] ); ?> <?php printf( __( '<cite class="fn">%s</cite> <span class="says">says:</span>' ), get_comment_author_link() ); ?> </div> <?php if ( $comment->comment_approved == '0' ) : ?> <em class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.' ); ?></em> <br /> <?php endif; ?> <div class="comment-meta commentmetadata"><a href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ); ?>"> <?php /* translators: 1: date, 2: time */ printf( __('%1$s at %2$s'), get_comment_date(), get_comment_time() ); ?></a><?php edit_comment_link( __( '(Edit)' ), ' ', '' ); ?> </div> <?php comment_text(); ?> <div class="reply"> <?php comment_reply_link( array_merge( $args, array( 'add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?> </div> <?php if ( 'div' != $args['style'] ) : ?> </div> <?php endif; ?> <?php }
如果你的主題添加修改了默認(rèn)的頭像調(diào)用方式,比如使用CN或者SSl方式調(diào)用,該方法將無效。