本文實(shí)例講述了WordPress小工具制作方法。分享給大家供大家參考,具體如下:
WordPress是一個(gè)擁有著無與倫比拓展性的軟件,它的側(cè)邊欄小工具很是方便。但是默認(rèn)的那幾個(gè)小工具完全不夠用,或者說樣式根本根本不能滿足需要。今天就講解一下如何制作一個(gè)小工具,然后接下來再給出一個(gè)評(píng)論小工具的制作實(shí)例。
小工具有三個(gè)部分,后臺(tái)顯示、數(shù)據(jù)保存、前臺(tái)顯示。當(dāng)然如果你的小工具不需要在后臺(tái)設(shè)置什么數(shù)據(jù),那數(shù)據(jù)保存可以省掉了。一般來講,一個(gè)小工具至少應(yīng)該有這三個(gè)部分。
小工具是一個(gè)類,像側(cè)邊欄一樣,你還得用代碼注冊(cè)它,它在能在后臺(tái)使用。
代碼如下:
class PostViews extends WP_Widget{
function PostViews(){
//這是定義小工具信息的函數(shù),也是類的構(gòu)建函數(shù)
}
function form($instance){
//這是表單函數(shù),也就是控制后臺(tái)顯示的
}
function update($new_instance,$old_instance){
//這是更新數(shù)據(jù)函數(shù),小工具如果有設(shè)置選項(xiàng),就需要保存更新數(shù)據(jù)
}
function widget($args,$instance){
//這是控制小工具前臺(tái)顯示的函數(shù)
}
}
function PostViews(){
//注冊(cè)小工具
register_widget('PostViews');
}
//widges_init,小工具初始化的時(shí)候執(zhí)行PostViews函數(shù),
add_action('widgets_init','PostViews');
根據(jù)代碼可知道,主要是繼承WordPress的WP_Widget類,并且重載里面的函數(shù),以此來達(dá)到自定義小工具的目的。
附:近期評(píng)論工具制作
WordPress其實(shí)自帶有一個(gè)近期評(píng)論的小工具,但是那個(gè)只有顯示誰在哪篇文章上面評(píng)論了,非常難看,根本不能滿足我們的需要。這次來說明的小工具可以顯示用戶頭像,評(píng)論內(nèi)容,已經(jīng)時(shí)間等各方面有用的信息。
還是和前面一樣,繼承 WP_Widget_Recent_Comments 類,代碼:
代碼如下:
* 繼承WP_Widget_Recent_Comments
* 這樣就只需要重寫widget方法就可以了
*/
class My_Widget_Recent_Comments extends WP_Widget_Recent_Comments {
/**
* 構(gòu)造方法,主要是定義小工具的名稱,介紹
*/
function My_Widget_Recent_Comments() {
$widget_ops = array('classname' => 'widget_recent_comment', 'description' => __('顯示最新評(píng)論內(nèi)容'));
$this->WP_Widget('my-recent-comments', __('我的最新評(píng)論', 'my'), $widget_ops);
}
/**
* 小工具的渲染方法,這里就是輸出評(píng)論
*/
function widget($args, $instance) {
global $wpdb, $comments, $comment;
$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;
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);
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 = convert_smilies($content);
//評(píng)論的文章
$post = '' . get_the_title($comment->comment_post_ID) . '';
//這里就是輸出的html,可以根據(jù)需要自行修改
$output .= ''
}
}
$output .= $after_widget;
echo $output;
$cache[$args['widget_id']] = $output;
wp_cache_set('my_widget_recent_comments', $cache, 'widget');
}
}
完了之后還要注冊(cè)小工具,這樣就可以在后臺(tái)拖動(dòng)了
代碼如下:
register_widget('My_Widget_Recent_Comments');
希望本文所述對(duì)大家基于wordpress的程序設(shè)計(jì)有所幫助。