自己做wordpress評(píng)論插件修改評(píng)論樣式(兩步美化評(píng)論內(nèi)容)

wordpress自帶的近期評(píng)論小工具不會(huì)顯示具體的評(píng)論內(nèi)容,而且還會(huì)顯示管理員的評(píng)論,感覺不是很好,只能自己處理一下?;私粋€(gè)晚上才處理好,主要用在理解小工具的原理上了,但是使用起來就非常簡單了,只要簡單的兩個(gè)步驟。該小工具在wordpress 3.4.1版本上測試通過。先來個(gè)截圖預(yù)覽下:
1、制作小工具
代碼一堆可以不用管它,直接將代碼保存到wordpress的/wp-content/widgets/comments.php文件。
為什么放到這里呢?因?yàn)橄裰黝}、插件這些都是存在wp-content這個(gè)目錄下面,小工具存放在這里可以統(tǒng)一管理,也符合wordpress目錄規(guī)則。
<?php</p> <p>/**
* 繼承WP_Widget_Recent_Comments
* 這樣就只需要重寫widget方法就可以了
*/
class My_Widget_Recent_Comments extends WP_Widget_Recent_Comments {</p> <p> /**
* 構(gòu)造方法,主要是定義小工具的名稱,介紹
*/
function My_Widget_Recent_Comments() {
$widget_ops = array('classname' => 'my_widget_recent_comments', 'description' => __('顯示最新評(píng)論內(nèi)容'));
$this->WP_Widget('my-recent-comments', __('我的最新評(píng)論', 'my'), $widget_ops);
}</p> <p> /**
* 小工具的渲染方法,這里就是輸出評(píng)論
*/
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;
//獲取評(píng)論,過濾掉管理員自己
$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();
//評(píng)論內(nèi)容
$content = apply_filters('get_comment_text', $comment->comment_content);
$content = mb_strimwidth(strip_tags($content), 0, '65', '...', 'UTF-8');
$content = convert_smilies($content);
//評(píng)論的文章
$post = '<a href="' . esc_url(get_comment_link($comment->comment_ID)) . '">' . get_the_title($comment->comment_post_ID) . '</a>';</p> <p> //這里就是輸出的html,可以根據(jù)需要自行修改
$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">發(fā)表在 ' . $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文件中加載這個(gè)小工具:
require(get_template_directory().'/../../widgets/comments.php');
2、進(jìn)入后臺(tái)管理拖入評(píng)論小工具
選擇外觀->小工具,可以看到評(píng)論小工具就加載進(jìn)來了,如下:
直接將評(píng)論小工具拖動(dòng)到側(cè)邊欄就OK了。
小結(jié)
有的朋友可能會(huì)擔(dān)心代碼出現(xiàn)什么問題,這個(gè)代碼是從系統(tǒng)自帶的評(píng)論小工具中復(fù)制過來的,主要是修改評(píng)論的獲取和評(píng)論的顯示樣式。系統(tǒng)自帶的評(píng)論小工具代碼可以參考/wp-includes/default-widgets.php中的WP_Widget_Recent_Comments類。
相關(guān)文章
CyberPanel安裝WordPress并配置偽靜態(tài)規(guī)則
下面教你如何在 CyberPanel安裝WordPress以及配置偽靜態(tài),需要的朋友可以參考下2023-12-27- 這篇文章主要介紹了wordpress無法安裝更新主題插件的解決辦法,需要的朋友可以參考下2020-12-27
WordPress必備數(shù)據(jù)庫SQL查詢語句整理
發(fā)現(xiàn)幾條比較實(shí)用的,適合 WordPress 實(shí)用的SQL語句。于是就趕緊收集分享出來了,需要的朋友可以參考下2017-09-23wordpress在安裝使用中出現(xiàn)404、403、500及502問題的分析與解決方法
wordpress是很多新手站長搭建個(gè)人博客最喜愛的程序,但是最近在使用WordPress的時(shí)候遇到了一些問題,所以想著將遇到問題總結(jié)分享出來,下面這篇文章主要給大家介紹了關(guān)于wo2017-08-11WordPress取消英文標(biāo)點(diǎn)符號(hào)自動(dòng)替換中文標(biāo)點(diǎn)符號(hào)的優(yōu)雅方法
這篇文章主要介紹了WordPress取消英文標(biāo)點(diǎn)符號(hào)自動(dòng)替換中文標(biāo)點(diǎn)符號(hào)的優(yōu)雅方法,需要的朋友可以參考下2017-04-04- 這篇文章主要給大家介紹了wordpress自定義上傳文件類型的方法,如WordPress默認(rèn)允許上傳 .exe 后綴名的可運(yùn)行文件,那么我們怎么禁止用戶在WordPress后臺(tái)發(fā)表文章時(shí)上傳 .e2016-12-19
- 大家可能發(fā)現(xiàn)了當(dāng)實(shí)現(xiàn)了前端用戶中心,后臺(tái)控制面板就失去了作用,那么限制其他用戶進(jìn)入后臺(tái)控制面板就很有必要了!那么我們要怎么做呢?通過下面這篇文章分享的方法后,只2016-12-19
WordPress實(shí)現(xiàn)回復(fù)文章評(píng)論后發(fā)送郵件通知的功能
這篇文章主要介紹了WordPress實(shí)現(xiàn)回復(fù)文章評(píng)論后發(fā)送郵件通知的功能,涉及wordpress針對(duì)評(píng)論與郵件的相關(guān)操作技巧,需要的朋友可以參考下2016-10-11WordPress使用自定義文章類型實(shí)現(xiàn)任意模板的方法
這篇文章主要介紹了WordPress使用自定義文章類型實(shí)現(xiàn)任意模板的方法,可通過自定義文章類型來實(shí)現(xiàn)任意模版的使用,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-10-11WordPress后臺(tái)地址被改導(dǎo)致無法登陸后臺(tái)的簡單解決方法
這篇文章主要介紹了WordPress后臺(tái)地址被改導(dǎo)致無法登陸后臺(tái)的簡單解決方法,簡單分析了后臺(tái)無法登陸的原因與相應(yīng)的解決方法,涉及針對(duì)wordpress配置項(xiàng)的簡單修改,需要的朋友2016-10-11