溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

為Gravatar頭像添加ALT屬性的方法

發(fā)布時(shí)間:2020-08-17 16:00:45 來源:億速云 閱讀:132 作者:小新 欄目:建站服務(wù)器

小編給大家分享一下為Gravatar頭像添加ALT屬性的方法,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!

圖片ALT屬性不僅有利于搜索引擎索引圖片,而且當(dāng)圖片無法加載的時(shí)候,會(huì)顯示圖片的ALT信息。

WordPress文章插入圖片時(shí)可以在“替代文本”中填寫ALT信息,但評論中的大量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ù)

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

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

替換為:

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

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

如果你的主題調(diào)用評論模模塊使用的函數(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)用,該方法將無效。

看完了這篇文章,相信你對為Gravatar頭像添加ALT屬性的方法有了一定的了解,想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI