为Gravatar头像添加ALT属性

WordPress283.2K阅读模式

为Gravatar头像添加ALT属性

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

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

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

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

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

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

替换为:

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

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

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

  1. wp_list_comments();

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

展开代码

展开收缩

  1. function mytheme_comment($comment$args$depth) {
  2.     if ( 'div' === $args['style'] ) {
  3.         $tag       = 'div';
  4.         $add_below = 'comment';
  5.     } else {
  6.         $tag       = 'li';
  7.         $add_below = 'div-comment';
  8.     }
  9.     ?>
  10.     <<?php echo $tag ?> <?php comment_class( emptyempty$args['has_children'] ) ? '' : 'parent' ) ?> id="comment-<?php comment_ID() ?>">
  11.     <?php if ( 'div' != $args['style'] ) : ?>
  12.         <div id="div-comment-<?php comment_ID() ?>" class="comment-body">
  13.     <?php endif; ?>
  14.     <div class="comment-author vcard">
  15.         <?php if ( $args['avatar_size'] != 0 ) echo get_avatar( $comment$args['avatar_size'] ); ?>
  16.         <?php printf( __( '<cite class="fn">%s</cite> <span class="says">says:</span>' ), get_comment_author_link() ); ?>
  17.     </div>
  18.     <?php if ( $comment->comment_approved == '0' ) : ?>
  19.          <em class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.' ); ?></em>
  20.           <br />
  21.     <?php endif; ?>
  22.     <div class="comment-meta commentmetadata"><a href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ); ?>">
  23.         <?php
  24.         /* translators: 1: date, 2: time */
  25.         printf( __('%1$s at %2$s'), get_comment_date(),  get_comment_time() ); ?></a><?php edit_comment_link( __( '(Edit)' ), '  ', '' );
  26.         ?>
  27.     </div>
  28.     <?php comment_text(); ?>
  29.     <div class="reply">
  30.         <?php comment_reply_link( array_merge$argsarray( 'add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
  31.     </div>
  32.     <?php if ( 'div' != $args['style'] ) : ?>
  33.     </div>
  34.     <?php endif; ?>
  35.     <?php
  36.     }

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

参考:

https://codex.wordpress.org/Function_Reference/wp_list_comments

https://codex.wordpress.org/Function_Reference/get_avatar

https://codex.wordpress.org/Plugin_API/Filter_Reference/get_avatar

本站文章大部分始于原创,用于个人学习记录,可能对您有所帮助,仅供参考!

weinxin
我的微信
版权声明
本站原创文章转载请注明文章出处及链接,谢谢合作!
 
知更鸟
  • WordPress
  • WordPress主题
  • functions.php
评论  28  访客  27  作者  1
    • Win7en乐园
      Win7en乐园 7

      沙发!真棒,鸟哥今天又有空写文章了!

      • Win7en乐园
        Win7en乐园 7

        不过我用的是多说呀,不敢奢求更多了。哎,加油吧,鸟哥!

          • 动感单车
            动感单车 7

            @ Win7en乐园 :roll: 就我个人认为,真心不喜欢使用多说,因为想要评论的话很嫌麻烦得要登陆才行,所以往往遇到这样的站点我基本不作评论(这也是我还从未在你博客评论过的原因,呵呵)!

          • 测试
            测试 1

            我有测试下http://zmingcx.com/wp-content/themes/begin/timthumb.php?src=http://ww2.sinaimg.cn/large/703be3b1jw1fadhsbvs5mj20kk0asgmr.jpg

            • Win7en乐园
              Win7en乐园 7

              鸟哥刚刚在测试离线发表文章嘛!!被我发现了

                • 知更鸟
                  知更鸟

                  @ Win7en乐园 真是洞察秋毫 :arrow:
                  有用户说主题不能离线发文章,测试一下

                    • Win7en乐园
                      Win7en乐园 7

                      @ 知更鸟 2011年的时候非常喜欢用windows live writer 进行离线发文章,优点就是不怕突然宕机,图片可以直接拖入。不过现在得益于wordpress的进步,自动保存,图片直接拖入浏览器窗口都完美解决。所以已经不再离线写作了。记得那时候鸟哥的主题可是为数不多的支持后台可视化写作的主题。我给鸟哥100分

                      • 老虎
                        老虎 6

                        @ 知更鸟 我也发现了。

                      • 励志语录
                        励志语录 6

                        @ Win7en乐园 :lol: 人才哈

                        • 动感单车
                          动感单车 7

                          @ Win7en乐园 :mrgreen: 看来你还是挺牛的嘛,向你学习,但不知有什么干货可以分享给我吗?最好是关于怎么做SEO的绝招!

                        • BanYuner
                          BanYuner 5

                          图片添加ALT属性确实是有必要的 我经常使用百度站长工具检测 就会有这个项目的检测

                            • 动感单车
                              动感单车 7

                              @ BanYuner 是啊,检测结果老是提示”图片没有alt属性“,非常令人烦!

                            • 森纯学生博客
                              森纯学生博客 1

                              好吧,学习到了

                              • 网赚
                                网赚 2

                                这个很必要啊

                                • Win7en乐园
                                  Win7en乐园 7

                                  鸟哥这两天是不是升级主题了啊,感觉鸟哥的网站打开速度快很多,以前的一些BUG也没有出现了。

                                  • 免费资源
                                    免费资源 4

                                    学习 感谢分享

                                    • 微部落博客
                                      微部落博客 3

                                      如果网站评论多的话,这个功能倒真是不错

                                      • PC在线云端
                                        PC在线云端 3

                                        这个功能在博客站中,还是有一点小作用的!

                                      匿名

                                      发表评论

                                      匿名网友
                                      :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

                                      拖动滑块以完成验证