侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

自己做wordpress评论插件修改评论样式(两步美化评论内容)

框架(架构)  /  管理员 发布于 7年前   577

wordpress自带的近期评论小工具不会显示具体的评论内容,而且还会显示管理员的评论,感觉不是很好,只能自己处理一下。花了近一个晚上才处理好,主要用在理解小工具的原理上了,但是使用起来就非常简单了,只要简单的两个步骤。该小工具在wordpress 3.4.1版本上测试通过。先来个截图预览下:

1、制作小工具

代码一堆可以不用管它,直接将代码保存到wordpress的/wp-content/widgets/comments.php文件。

为什么放到这里呢?因为像主题、插件这些都是存在wp-content这个目录下面,小工具存放在这里可以统一管理,也符合wordpress目录规则。


复制代码代码如下:
<?php</p><p>/**
* 继承WP_Widget_Recent_Comments
* 这样就只需要重写widget方法就可以了
*/
class My_Widget_Recent_Comments extends WP_Widget_Recent_Comments {</p><p> /**
* 构造方法,主要是定义小工具的名称,介绍
*/
function My_Widget_Recent_Comments() {
$widget_ops = array('classname' => 'my_widget_recent_comments', 'description' => __('显示最新评论内容'));
$this->WP_Widget('my-recent-comments', __('我的最新评论', 'my'), $widget_ops);
}</p><p> /**
* 小工具的渲染方法,这里就是输出评论
*/
function widget($args, $instance) {
global $wpdb, $comments, $comment;</p><p> $cache = wp_cache_get('my_widget_recent_comments', 'widget');</p><p> if (!is_array($cache))
$cache = array();</p><p> if (!isset($args['widget_id']))
$args['widget_id'] = $this->id;</p><p> if (isset($cache[$args['widget_id']])) {
echo $cache[$args['widget_id']];
return;
}</p><p> extract($args, EXTR_SKIP);
$output = '';
$title = apply_filters('widget_title', empty($instance['title']) ? __('Recent Comments') : $instance['title'], $instance, $this->id_base);
if (empty($instance['number']) || !$number = absint($instance['number']))
$number = 5;
//获取评论,过滤掉管理员自己
$comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE user_id !=2 and comment_approved = '1' and comment_type not in ('pingback','trackback') ORDER BY comment_date_gmt DESC LIMIT $number");
$output .= $before_widget;
if ($title)
$output .= $before_title . $title . $after_title;</p><p> $output .= '<ul id="myrecentcomments">';
if ($comments) {
// Prime cache for associated posts. (Prime post term cache if we need it for permalinks.)
$post_ids = array_unique(wp_list_pluck($comments, 'comment_post_ID'));
_prime_post_caches($post_ids, strpos(get_option('permalink_structure'), '%category%'), false);</p><p>foreach ((array) $comments as $comment) {
//头像
$avatar = get_avatar($comment, 40);
//作者名称
$author = get_comment_author();
//评论内容
$content = apply_filters('get_comment_text', $comment->comment_content);
$content = mb_strimwidth(strip_tags($content), 0, '65', '...', 'UTF-8');
$content = convert_smilies($content);
//评论的文章
$post = '<a href="https:/cms/' . esc_url(get_comment_link($comment->comment_ID)) . '">' . get_the_title($comment->comment_post_ID) . '</a>';</p><p> //这里就是输出的html,可以根据需要自行修改
$output .= '<li class="comment" style="padding-bottom: 5px; ">
<div>
<table class="tablayout"><tbody><tr>
<td class="tdleft" style="width:55px;vertical-align:top;">' . $avatar . '</td>
<td class="tdleft" style="vertical-align:top;">
<p class="comment-author"><strong><span class="fn">' . $author . '</span></strong> <span class="says">发表在 ' . $post . '</span></p>
</tr></tbody></table>
</div>
<div class="comment-content"><p class="last">' . $content . '</p>
</div>
</li>';
}
}
$output .= '</ul>';
$output .= $after_widget;</p><p> echo $output;
$cache[$args['widget_id']] = $output;
wp_cache_set('my_widget_recent_comments', $cache, 'widget');
}</p><p>}</p><p>//注册小工具
register_widget('My_Widget_Recent_Comments');

在主题目录下的funtions.php文件中加载这个小工具:


复制代码代码如下:
require(get_template_directory().'/../../widgets/comments.php');

2、进入后台管理拖入评论小工具

选择外观->小工具,可以看到评论小工具就加载进来了,如下:



直接将评论小工具拖动到侧边栏就OK了。

小结

有的朋友可能会担心代码出现什么问题,这个代码是从系统自带的评论小工具中复制过来的,主要是修改评论的获取和评论的显示样式。系统自带的评论小工具代码可以参考/wp-includes/default-widgets.php中的WP_Widget_Recent_Comments类。


  • 上一条:
    wordpress获取文章评论数过滤掉作者代码分享
    下一条:
    修改wordpress上传临时目录解决wordpress无法安装插件包的方法
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Filament v3.1版本发布(0个评论)
    • docker + gitea搭建一个git服务器流程步骤(0个评论)
    • websocket的三种架构方式使用优缺点浅析(0个评论)
    • ubuntu20.4系统中宿主机安装nginx服务,docker容器中安装php8.2实现运行laravel10框架网站(0个评论)
    • phpstudy_pro(小皮面板)中安装最新php8.2.9版本流程步骤(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2018-05
    • 2020-02
    • 2020-03
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-11
    • 2021-03
    • 2021-09
    • 2021-10
    • 2021-11
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-08
    • 2023-08
    • 2023-10
    • 2023-12
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客