如何给 WordPress 网站的 Gravatar 头像添加 alt 属性?

如何给 WordPress 网站的 Gravatar 头像添加 alt 属性?图片ALT属性不仅有利于搜索引擎索引图片,而且当图片无法加载的时候,会显示图片的ALT信息。WordPress文章插入图片时可以在“替代文本”中填写ALT信息,但评论中的大量Gravatar头像一般主题都没有ALT属性,其实WP以为我们预设了Gravatar头像ALT属性参数。

1、查看WP官网 Codex  get avatar  默认的可选参数:

  1. <?php echo get_avatar( $id_or_email, $size, $default, $alt, $args ); ?>

其中:$alt 就是 alt可选参数

2、打开主题评论模板,找到类似这句:

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

替换为:

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

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

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

  1. wp_list_comments();

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

  1. 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 }

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

所属分类:WordPress

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秃头程序员宝贝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值