不用插件实现具有Gravatar头像功能的最新留言

WordPress106.8K阅读模式

可能大部Wordpress用户为了美化主题外观,都会使用WP-RecentComments插件增强最近留言显示功能,该插件最大的特色就是可以显示留言Gravatar头像,但还是那句话:越少的使用插件,却能让使用者完全体验到主题的各项功能,才是真正好的主题!一段函数代码就能实现的功能,何必调用烦杂的插件。下面的两段函数代码就可以完全替代留言增强插件。
这两段函数代码,分别具有显示日志作者的留言和不显示作者留言功能,为 什么要去掉作者的留言呢,道理很简单,一般留言都显示在侧边,如果设置显示10条留言,正好有10位浏览者留言,而作者可能在同一时间内分别进行回复,结 果就是侧边显示的留言全部是作者自己的,不利于站长及时查看是否有新留言并回复。

代码一 :这段代码显示日志作者及浏览者的留言

  1. <?php
  2. global $wpdb;
  3. $sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author, comment_date_gmt, comment_approved, comment_type,comment_author_url,comment_author_email, SUBSTRING(comment_content,1,14) AS com_excerpt FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) WHERE comment_approved = '1' AND comment_type = '' AND post_password = '' AND user_id='0' ORDER BY comment_date_gmt DESC LIMIT 10";
  4. $comments = $wpdb->get_results($sql);
  5. $output = $pre_HTML;
  6. foreach ($comments as $comment) {$output .= "\n<li>".get_avatar(get_comment_author_email('comment_author_email'), 32).strip_tags($comment->comment_author).":<br />" . " <a href=\"" . get_permalink($comment->ID) ."#comment-" . $comment->comment_ID . "\" title=\"on " .$comment->post_title . "\">" . strip_tags($comment->com_excerpt)."</a></li>";}
  7. $output .= $post_HTML;
  8. echo $output;?>

代码二: 这段代码不显示日志作者留言,只显示浏览者的留言

  1. <?php
  2. global $wpdb;
  3. $sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author, comment_date_gmt, comment_approved, comment_type,comment_author_url,comment_author_email, SUBSTRING(comment_content,1,14) AS com_excerpt FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) WHERE comment_approved = '1' AND comment_type = '' AND post_password = '' AND user_id='0' ORDER BY comment_date_gmt DESC LIMIT 10";
  4. $comments = $wpdb->get_results($sql);
  5. $output = $pre_HTML;
  6. foreach ($comments as $comment) {$output .= "\n<li>".get_avatar(get_comment_author_email('comment_author_email'), 32).strip_tags($comment->comment_author).":<br />" . " <a href=\"" . get_permalink($comment->ID) ."#comment-" . $comment->comment_ID . "\" title=\"on " .$comment->post_title . "\">" . strip_tags($comment->com_excerpt)."</a></li>";}
  7. $output .= $post_HTML;
  8. echo $output;?>

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

weinxin
我的微信
版权声明
本站原创文章转载请注明文章出处及链接,谢谢合作!
 
知更鸟
评论  10  访客  6  作者  4
    • Boss方
      Boss方 4

      用到哪里啊??
      直接添加到sidebar.php还是在侧边栏小工具新建文本工具粘贴呢?

        • 知更鸟
          知更鸟

          @ Boss方 热点新闻主题侧边留言的r_comments.php模板文件就是第二种

            • Boss方
              Boss方 4

              @ 知更鸟 恩,热点新闻主题用这个我知道的。。
              我如果在其他主要要用这个第二种,是直接在侧边栏用文本添加还是在sidebar.php里写代码啊?

                • 知更鸟
                  知更鸟

                  @ Boss方 文本小工具不能添加函数,需写在sidebar中,并且要配合相应的CSS

            • 岚
              3

              能自己写是最好的了。。。
              插件左装一个右一个,应接不暇,看得我想要吐血,呵呵

              • jinxuan
                jinxuan 0

                系统越来越大,插件越来越多。简单而短小的应用还是自己写写在function.php比较好。

                • 网络过客
                  网络过客 4

                  我现在租的没有为静态的主机,说要我另外租一个服务器,我现在哪里能租得起啊,一个月最低也要600元/月啊,老大你的是哪里租的啊,我的空间能跟你的租在一起吗价格怎么样!~

                    • 知更鸟
                      知更鸟

                      @ 网络过客 我用的当然是WOPUS的空间,Linode 主机,不是很贵
                      http://idc.wopus.org/vps
                      国内的空间更便宜
                      http://idc.wopus.org/

                    • 网络过客
                      网络过客 4

                      那你说像我这样的新手是不是用国内空间就好了!~

                        • 知更鸟
                          知更鸟

                          @ 网络过客 嗯,国内的空间速度比较快,不过域名需备案
                          国外的就不用了

                      匿名

                      发表评论

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

                      拖动滑块以完成验证