欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

  發(fā)布時(shí)間:2013-12-25 10:00:03   作者:佚名   我要評(píng)論
wordpress自帶的近期評(píng)論小工具不會(huì)顯示具體的評(píng)論內(nèi)容,而且還會(huì)顯示管理員的評(píng)論,感覺不是很好,只能自己處理一下,這個(gè)代碼是從系統(tǒng)自帶的評(píng)論小工具中復(fù)制過來的,主要是修改評(píng)論的獲取和評(píng)論的顯示樣式

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ī)則。


復(fù)制代碼
代碼如下:

<?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è)小工具:


復(fù)制代碼
代碼如下:

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)文章

最新評(píng)論