枫叶不再红 發表於 2023-9-27 00:00:00

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

<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
wordpress自带的近期评论小工具不会显示具体的评论内容,而且还会显示管理员的评论,感觉不是很好,只能自己处理一下。花了近一个晚上才处理好,主要用在理解小工具的原理上了,但是使用起来就非常简单了,只要简单的两个步骤。该小工具在wordpress 3.4.1版本上测试通过。先来个截图预览下:<br><br><img title="自己做wordpress评论插件修改评论样式(两步美化评论内容)" alt="自己做wordpress评论插件修改评论样式(两步美化评论内容)" id="theimg" src="https://zhuji.jb51.net/uploads/img/202305/d1b8d72e89993c0a3627e713fac0ac8a.jpg" style="max-width:100%!important;height:auto!important;border: 1px solid rgb(204, 204, 204); vertical-align: middle; padding: 1px; overflow: hidden; max-width: 696px; width: 305px; height: 449px;"></p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
1、制作小工具</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
代码一堆可以不用管它,直接将代码保存到wordpress的/wp-content/widgets/comments.php文件。</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
为什么放到这里呢?因为像主题、插件这些都是存在wp-content这个目录下面,小工具存放在这里可以统一管理,也符合wordpress目录规则。</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
 </p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
<p>
代码如下:</p>
</div>
<div id="phpcode2" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
&lt;?php&lt;/p&gt; &lt;p&gt;/**<br>
* 继承WP_Widget_Recent_Comments<br>
* 这样就只需要重写widget方法就可以了<br>
*/<br>
class My_Widget_Recent_Comments extends WP_Widget_Recent_Comments {&lt;/p&gt; &lt;p&gt; /**<br>
* 构造方法,主要是定义小工具的名称,介绍<br>
*/<br>
function My_Widget_Recent_Comments() {<br>
$widget_ops = array('classname' =&gt; 'my_widget_recent_comments', 'description' =&gt; __('显示最新评论内容'));<br>
$this-&gt;WP_Widget('my-recent-comments', __('我的最新评论', 'my'), $widget_ops);<br>
}&lt;/p&gt; &lt;p&gt; /**<br>
* 小工具的渲染方法,这里就是输出评论<br>
*/<br>
function widget($args, $instance) {<br>
global $wpdb, $comments, $comment;&lt;/p&gt; &lt;p&gt; $cache = wp_cache_get('my_widget_recent_comments', 'widget');&lt;/p&gt; &lt;p&gt; if (!is_array($cache))<br>
$cache = array();&lt;/p&gt; &lt;p&gt; if (!isset($args['widget_id']))<br>
$args['widget_id'] = $this-&gt;id;&lt;/p&gt; &lt;p&gt; if (isset($cache[$args['widget_id']])) {<br>
echo $cache[$args['widget_id']];<br>
return;<br>
}&lt;/p&gt; &lt;p&gt; extract($args, EXTR_SKIP);<br>
$output = '';<br>
$title = apply_filters('widget_title', empty($instance['title']) ? __('Recent Comments') : $instance['title'], $instance, $this-&gt;id_base);<br>
if (empty($instance['number']) || !$number = absint($instance['number']))<br>
$number = 5;<br>
//获取评论,过滤掉管理员自己<br>
$comments = $wpdb-&gt;get_results("SELECT * FROM $wpdb-&gt;comments WHERE user_id !=2 and comment_approved = '1' and comment_type not in ('pingback','trackback') ORDER BY comment_date_gmt DESC LIMIT $number");<br>
$output .= $before_widget;<br>
if ($title)<br>
$output .= $before_title . $title . $after_title;&lt;/p&gt; &lt;p&gt; $output .= '&lt;ul id="myrecentcomments"&gt;';<br>
if ($comments) {<br>
// Prime cache for associated posts. (Prime post term cache if we need it for permalinks.)<br>
$post_ids = array_unique(wp_list_pluck($comments, 'comment_post_ID'));<br>
_prime_post_caches($post_ids, strpos(get_option('permalink_structure'), '%category%'), false);&lt;/p&gt; &lt;p&gt; foreach ((array) $comments as $comment) {<br>
//头像<br>
$avatar = get_avatar($comment, 40);<br>
//作者名称<br>
$author = get_comment_author();<br>
//评论内容<br>
$content = apply_filters('get_comment_text', $comment-&gt;comment_content);<br>
$content = mb_strimwidth(strip_tags($content), 0, '65', '...', 'UTF-8');<br>
$content = convert_smilies($content);<br>
//评论的文章<br>
$post = '&lt;a href="' . esc_url(get_comment_link($comment-&gt;comment_ID)) . '"&gt;' . get_the_title($comment-&gt;comment_post_ID) . '&lt;/a&gt;';&lt;/p&gt; &lt;p&gt; //这里就是输出的html,可以根据需要自行修改<br>
$output .= '&lt;li style="padding-bottom: 5px; "&gt;<br>
&lt;div&gt;<br>
&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;<br>
&lt;td style="width:55px;vertical-align:top;"&gt;' . $avatar . '&lt;/td&gt;<br>
&lt;td style="vertical-align:top;"&gt;<br>
&lt;p&gt;&lt;strong&gt;&lt;span&gt;' . $author . '&lt;/span&gt;&lt;/strong&gt; &lt;span&gt;发表在 ' . $post . '&lt;/span&gt;&lt;/p&gt;<br>
&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;<br>
&lt;/div&gt;<br>
&lt;div&gt;&lt;p&gt;' . $content . '&lt;/p&gt;<br>
&lt;/div&gt;<br>
&lt;/li&gt;';<br>
}<br>
}<br>
$output .= '&lt;/ul&gt;';<br>
$output .= $after_widget;&lt;/p&gt; &lt;p&gt; echo $output;<br>
$cache[$args['widget_id']] = $output;<br>
wp_cache_set('my_widget_recent_comments', $cache, 'widget');<br>
}&lt;/p&gt; &lt;p&gt;}&lt;/p&gt; &lt;p&gt;//注册小工具<br>
register_widget('My_Widget_Recent_Comments');</div>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
 </p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
在主题目录下的funtions.php文件中加载这个小工具:</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
 </p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
<p>
代码如下:</p>
</div>
<div id="phpcode3" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
require(get_template_directory().'/../../widgets/comments.php');</div>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
 </p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
2、进入后台管理拖入评论小工具</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
选择外观-&gt;小工具,可以看到评论小工具就加载进来了,如下:<br><br><img title="自己做wordpress评论插件修改评论样式(两步美化评论内容)" alt="自己做wordpress评论插件修改评论样式(两步美化评论内容)" id="theimg" src="https://zhuji.jb51.net/uploads/img/202305/38a594b618115d1d60ce70be9d78259f.jpg" style="max-width:100%!important;height:auto!important;border: 1px solid rgb(204, 204, 204); vertical-align: middle; padding: 1px; overflow: hidden; max-width: 696px; width: 264px; height: 212px;"><br><br>
直接将评论小工具拖动到侧边栏就OK了。</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
小结</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
有的朋友可能会担心代码出现什么问题,这个代码是从系统自带的评论小工具中复制过来的,主要是修改评论的获取和评论的显示样式。系统自带的评论小工具代码可以参考/wp-includes/default-widgets.php中的WP_Widget_Recent_Comments类。</p>
頁: [1]
查看完整版本: 自己做wordpress评论插件修改评论样式(两步美化评论内容)