首頁  >  文章  >  CMS教程  >  如何為Gravatar頭像新增ALT屬性

如何為Gravatar頭像新增ALT屬性

藏色散人
藏色散人轉載
2019-12-17 11:32:182459瀏覽

下面由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 ( &#39;div&#39; === $args[&#39;style&#39;] ) {
        $tag       = &#39;div&#39;;
        $add_below = &#39;comment&#39;;
    } else {
        $tag       = &#39;li&#39;;
        $add_below = &#39;div-comment&#39;;
    }
    ?>
    <<?php echo $tag ?> <?php comment_class( emptyempty( $args[&#39;has_children&#39;] ) ? &#39;&#39; : &#39;parent&#39; ) ?> id="comment-<?php comment_ID() ?>">
    <?php if ( &#39;div&#39; != $args[&#39;style&#39;] ) : ?>
        <div id="div-comment-<?php comment_ID() ?>" class="comment-body">
    <?php endif; ?>
    <div class="comment-author vcard">
        <?php if ( $args[&#39;avatar_size&#39;] != 0 ) echo get_avatar( $comment, $args[&#39;avatar_size&#39;] ); ?>
        <?php printf( __( &#39;<cite class="fn">%s</cite> <span class="says">says:</span>&#39; ), get_comment_author_link() ); ?>
    </div>
    <?php if ( $comment->comment_approved == &#39;0&#39; ) : ?>
         <em class="comment-awaiting-moderation"><?php _e( &#39;Your comment is awaiting moderation.&#39; ); ?></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( __(&#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 class="reply">
        <?php comment_reply_link( array_merge( $args, array( &#39;add_below&#39; => $add_below, &#39;depth&#39; => $depth, &#39;max_depth&#39; => $args[&#39;max_depth&#39;] ) ) ); ?>
    </div>
    <?php if ( &#39;div&#39; != $args[&#39;style&#39;] ) : ?>
    </div>
    <?php endif; ?>
    <?php
    }

如果你的主題添加修改了預設的頭像調用方式,例如使用CN或SSl方式調用,該方法將無效。

以上是如何為Gravatar頭像新增ALT屬性的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:zmingcx.com。如有侵權,請聯絡admin@php.cn刪除