用代码实现wordpress小工具,有分类的最新文章小工具
文章目录
因为wordpress里面的小工具,并没有这项功能。
大部分要让小工具有「分类的最新文章」
都是用外挂实现。
外挂很麻烦,我们能用代码就用代码。作法很简单。
打开你主题的functions.php
加入以下代码
[cc lang="php"] //分类的最新文章小工具function register_catlist_widget(){ register_widget('catlist_widget'); } add_action('widgets_init','register_catlist_widget'); class Catlist_Widget extends wp_Widget{ /** * 定义wp_Widget类型,设置小工具名和描述* Sets up the widget name and description */ public function __construct(){ $widget_ops = array('classname' => 'catlist_widget', 'description' => '指定分类目录与显示数目' ); // parent::__construct()调用定义函数parent::__construct( 'category-post-list', // Base ID __('指定分类目录文章列表','text_domain'), // Name $ widget_ops // Args ); // add_action( $tag, $function_to_add); add_action( 'save_post', array($this, 'flush_widget_cache') ); add_action( 'deleted_post', array($this, 'flush_widget_cache') ) ; add_action( 'switch_theme', array($this, 'flush_widget_cache') ); } /** * Outputs the content of the widget * @param [array] $args [description] * @param [array] $instance [description ] * @return [type] [description] */ public function widget($args,$instance){ $cache = array(); if ( ! $this->is_preview() ) { $cache = wp_cache_get( 'catlist_posts', ' widget' ); } if ( ! is_array( $cache ) ) { $cache = array(); } if ( ! isset( $args['widget_id'] ) ) { $args['widget_id'] = $this-> id; } if ( isset( $cache[ $args['widget_id'] ] ) ) { echo $cache[ $args['widget_id'] ]; return; } ob_start(); $title = ( ! empty( $instance ['title'] ) ) ? $instance['title'] : '分类文章列表'; $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); $number = ( ! empty ( $instance['num'] ) ) ? absint( $instance['num'] ) : 10; if ( ! $number ) $number = 10; $catid = ( ! empty( $instance['cat'] ) ) ? absint( $instance['cat'] ) : 1; if ( ! $catid ) $catid = 1; global $post; $r = new wp_Query( apply_filters('widget_posts_args', array( 'posts_per_page' => $ number, 'cat' => $catid, 'post_status' => 'publish', 'no_found_rows' => true ))); if ($r->have_posts()) : ?>
- have_posts() ) : $r->the_post(); ?>
is_preview() ) { $cache[ $args['widget_id'] ] = ob_get_flush(); wp_cache_set( 'catlist_posts', $cache, 'widget' ); } else { ob_end_flush(); } } public function update( $new_instance, $old_instance ) { $instance = $old_instance; $instance['title'] = strip_tags($new_instance['title']); $instance['num'] = (int) $new_instance['number']; $instance['cat'] = (int) $new_instance['cat']; $this->flush_widget_cache(); $alloptions = wp_cache_get( 'alloptions', 'options' ); if ( isset($alloptions['catlist_widget']) ) delete_option('catlist_widget'); return $instance; } public function flush_widget_cache() { wp_cache_delete('catlist_posts', 'widget'); } /** * Back-end widget form. * @see wp_Widget::form() * @param [array] $instance [description] * @return [type] [description] */ public function form($instance){ $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : ''; $number = isset( $instance['num'] ) ? absint( $instance['num'] ) : 10; $catid = isset( $instance['cat']) ? absint( $instance['cat'] ):''; ?>
http://dhamma.com.tw/img/4040.png
的图片可以改成你要的,大小是40*40
在你的主题里面新增css
[cc lang="php"] /******************** 小工具的分类最新文章*************** ******/ .sidebar-blog-recent.icon-style .post-type {width: 40px;height: 40px;text-align: center;color: #ffffff;font-size: 16px;float: left ; } .sidebar-blog-recent.icon-style .post-type i {margin:0;} .sidebar-blog-recent.icon-style .post-content {border-bottom: 2px solid #0017A6;margin-left : 46px;color:#888;} .sidebar-blog-recent.icon-style .entry-title {margin:0 0 20px;} .sidebar-blog-recent.icon-style .comments-link {float:right; } .sidebar-blog-recent.icon-style .post-content a {color:#C400C9;} .sidebar-blog-recent.icon-style .post-content a:hover {color:#cc3333;} [/cc ]