搜尋
首頁CMS教程&#&按如何為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 ( &#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。如有侵權,請聯絡admin@php.cn刪除
WordPress與其他流行的CMS平台相比如何?WordPress與其他流行的CMS平台相比如何?May 02, 2025 am 12:18 AM

WordPressExcccelineaseeandAdaptability,MakeitItiDealForBeginnersandsMallToMedium-SizedBusinesses.1)siseofuse:wordpressisuser-Frylyly.2)安全:drupalleadswithstrongsecurityfeatures.3)性能:performance:performance formation:phast offersefersefersefersefersefersefersefersefersexcellentperformanceedueTonode.sscore.jssor.jjsy.jjsy.4)4)

您可以使用WordPress構建會員網站嗎?您可以使用WordPress構建會員網站嗎?May 01, 2025 am 12:08 AM

是的,您可以使用Plypluginslikememberpress,PayMembersubScriptions,OrwooCommerceForuserManagemention,ContentAccesControl,andPaymentMenthandling.2)

WordPress是否需要編碼知識作為CMS?WordPress是否需要編碼知識作為CMS?Apr 30, 2025 am 12:03 AM

你不需要編程知識就能使用WordPress,但掌握編程可以提升體驗。 1)使用CSS和HTML可以調整主題樣式。 2)PHP知識能編輯主題文件,添加功能。 3)自定義插件和元標籤可優化SEO。 4)注意備份和使用子主題以防更新問題。

使用WordPress時的安全考慮是什麼?使用WordPress時的安全考慮是什麼?Apr 29, 2025 am 12:01 AM

TosecureaWordPresssite,followthesesteps:1)RegularlyupdateWordPresscore,themes,andpluginstopatchvulnerabilities.2)Usestrong,uniquepasswordsandenabletwo-factorauthentication.3)OptformanagedWordPresshostingorsecuresharedhostingwithawebapplicationfirewal

WordPress與其他網站構建者相比如何?WordPress與其他網站構建者相比如何?Apr 28, 2025 am 12:04 AM

WordPressExcelSoverotherWeberSiteBuilderSduetoItsflexible,可伸縮性,andopen-sourcenature.1)它'saversatilecmswithExtEnsextEnsexenSiveCustomizedOptionsVIATHEMESANDPLUGINS.2)它的alllearbutoffersbutoffersbutoffersbutoffersbutefersbuterbutfulcontrololoncemastered.3)

5個WordPress插件,供開發人員在2025年使用5個WordPress插件,供開發人員在2025年使用Apr 27, 2025 am 08:25 AM

2025年網站開發的七個必備WordPress插件 在2025年建立頂級WordPress網站需要速度,響應能力和可擴展性。 實現這種有效的實現通常取決於戰略插件的選擇。 這篇文章Highlig

您將使用WordPress做什麼?您將使用WordPress做什麼?Apr 27, 2025 am 12:14 AM

WordPresscanbeusedforvariouspurposesbeyondblogging.1)E-commerce:WithWooCommerce,itcanbecomeafullonlinestore.2)Membershipsites:PluginslikeMemberPressenableexclusivecontentareas.3)Portfoliosites:ThemeslikeAstraallowstunninglayouts.Ensuretomanageplugins

WordPress適合創建投資組合網站嗎?WordPress適合創建投資組合網站嗎?Apr 26, 2025 am 12:05 AM

是的,wordpressisisexcellentforcortingaportfoliowebsite.1)itoffersnumeroversnumeroverportfolio-spificthemeslike'astra'astra'astra'astra'astra'astra'astra'astra'astra'elementor'Enelementor'enableIntiviveSiveSign,Thoughtemanycanslowthesite.3)

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。