WordPress文章中如何为Gravatar头像添加ALT属性?

图片Alt属性不仅有利于搜索引擎索引图片,而且当图片无法加载的时候,会显示图片的ALT信息。下面给大家介绍为Gravatar头像添加ALT属性的方法,希望对需要的朋友有所帮助!WordPress文章插入图片时可以在“替代文本”中填写ALT信息,但评论中的大量Gravatar头像一般主题都没有Alt属性,其实WordPr...

图片Alt属性不仅有利于搜索引擎索引图片,而且当图片无法加载的时候,会显示图片的ALT信息。下面给大家介绍为Gravatar头像添加ALT属性的方法,希望对需要的朋友有所帮助!

WordPress文章插入图片时可以在“替代文本”中填写ALT信息,但评论中的大量Gravatar头像一般主题都没有Alt属性,其实WordPress以为我们预设了Gravatar头像Alt属性参数。

WordPress文章中如何为Gravatar头像添加ALT属性?

查看WordPress官网 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, '', get_comment_author() ); ?>

将评论者名称作为Alt属性。

如果你的主题调用评论模模块使用的函数是:

wp_list_comments();

暂时在官网上还没找到用该函数添加Alt属性的参数(貌似WordPress默认主题ALT也是空的),只能按下面的代码拆分这个函数,然后修改。

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%20echo%20htmlspecialchars(%20get_comment_link(%20$comment->comment_ID%20)%20);%20?>">        <?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    }

如果你的WordPress主题添加修改了默认的头像调用方式,比如使用CN或者SSl方式调用,该方法将无效。

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA打赏
共{{data.count}}人
人已打赏
wordpress教程

荆门网站建设选择wordpress主题 高端易操作成本低

2023-2-17 0:00:36

wordpress教程

如何提高搜索引擎对自身网站的青睐度?

2023-2-17 0:00:38

!
你也想出现在这里?立即 联系我们吧!
信息
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
搜索