WP Widget: 带分类过滤功能的最新文章

» 2012-12-27 39条评论

这是一个朋友需要的小功能,扔这里,也许有人需要。

其实就是对最新文章代码加个分类过滤而已,做成小工具只是方便安排侧边栏的展示位置。

WordPress 小工具:带分类过滤功能的最新文章
1. 支持文章数量设置(废话),默认是5篇
2. 有所有分类下拉框直接选择分类,方便(懒人)
3. 没了,简单的功能

方法:

1. 把下面的代码扔进所用主题的 functions.php,当然肯定是在 <php ... ?> 里面

//////// 带过滤功能的最新文章
//Adds zww_recent_posts_widget widget.
class zww_recent_posts_widget extends WP_Widget {
	//Register widget with WordPress.
	public function __construct() {
		parent::__construct(
	 		'zww_recent_posts_widget', // Base ID
			'ZWW\'s 最新文章', // Name
			array( 'description' => '带分类过滤功能的最新文章', ) // Args
		);
	}
	public function widget( $args, $instance ) {
		extract( $args );
		$title = apply_filters( 'widget_title', $instance['title'] );
		$post_num = apply_filters( 'widget_title', $instance['post_num'] );
		$catID = apply_filters( 'widget_title', $instance['catID'] );

		echo $before_widget;

		if ( ! empty( $title ) )
			echo $before_title . $title . $after_title; ?>

		<ul>
			<?php
			global $post;
			$tmp_post = $post;
			$recent_posts = get_posts('orderby=ASC&numberposts='.$post_num.'&category='.$catID);
			foreach( $recent_posts as $post ) { setup_postdata($post); ?>
				<li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
			<?php } $post = $tmp_post; setup_postdata($post); ?>
		</ul>

		<?php
		echo $after_widget;
	}

	public function update( $new_instance, $old_instance ) {
		$instance = array();
		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['post_num'] = strip_tags( $new_instance['post_num'] );
		$instance['catID'] = strip_tags( $new_instance['catID'] );
		return $instance;
	}

	public function form( $instance ) {
		if ( isset( $instance[ 'title' ] ) ) {
			$title = $instance[ 'title' ];
		}
		else {
			$title = 'Recent Posts';
		}
		if ( isset( $instance[ 'post_num' ] ) ) {
			$post_num = $instance[ 'post_num' ];
		}
		else {
			$post_num = 5;
		}
		if ( isset( $instance[ 'catID' ] ) ) {
			$catID = $instance[ 'catID' ];
		}
		else {
			$catID = '';
		}
		?>
		<p>
			<label for="<?php echo $this->get_field_id( 'title' ); ?>">标题:</label> 
			<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
			<label for="<?php echo $this->get_field_id( 'post_num' ); ?>">显示数量:</label> 
			<input class="widefat" id="<?php echo $this->get_field_id( 'post_num' ); ?>" name="<?php echo $this->get_field_name( 'post_num' ); ?>" type="text" value="<?php echo esc_attr( $post_num ); ?>" />
			<label for="<?php echo $this->get_field_id( 'slug' ); ?>">选择分类:</label> 
			<?php
			wp_dropdown_categories(array(
				'name' => $this->get_field_name( 'catID' ),
				'hide_empty' => 0,
				'orderby' => 'name',
				'show_count' => 1,
				'selected' => esc_attr( $catID ),
				'hierarchical' => true,
				'show_option_none' =>'全部分类',
				'echo' => 1
				));
				?>
		</p>
		<?php 
	}
} // class zww_recent_posts_widget
// register zww_recent_posts_widget widget
add_action( 'widgets_init', create_function( '', 'register_widget( "zww_recent_posts_widget" );' ) );

2. 然后进“外观 》小工具”,找到如下图,拖到右边的侧边栏小工具区域

121227-zww-recent-posts-widget-1

3. 再然后,你懂的

121227-zww-recent-posts-widget-2

就这样。

我的 Nexus 4 神马时候才能发货啊?Google!!!都一个月了!!!

zww
or
oooo

“WP Widget: 带分类过滤功能的最新文章”有39条评论

  1. 小闇 says:

    沙發不解釋…

    1. zwwooooo says:

      @小闇
      @野蛮人
      我接上:不回复也没关系。好顺~

    2. 小闇 says:

      @zwwooooo
      不行不行,既然你佔了我家文章的沙發,禮尚往來 :mrgreen: :mrgreen: :mrgreen:

  2. 野蛮人 says:

    板凳。不解释。。。

  3. Rancho says:

    前排。。。。。

  4. 西门 says:

    靠背~~~ :mrgreen:

  5. 混生 says:

    此博客体验很舒服,呵呵

  6. 折腾的大牛! 佩服佩服! 最近我要写学校的网站和小型系统 快愁死了!

    1. zwwooooo says:

      @所谓刚子
      有木有工资啊

    2. @zwwooooo
      有啊,就是没人做!找人都找不到!

    3. zwwooooo says:

      @所谓刚子
      有就别抱怨了,哈

  7. 灰狼 says:

    还算前排吧?插入一个

  8. 哼哼猪 says:

    调用某一分类的文章,貌似这个不错

  9. 这个太棒了,下次做主题就用上。以前还要用postview插件神马的,如今不用啦。 :lol:

  10. 大发 says:

    这个以前我也弄过,只不过需要自己输入分类的ID

    1. zwwooooo says:

      @大发
      懂得自己写就行了

  11. W3Cshare says:

    路过,不解释 :mrgreen:

  12. 哈哈 原来还没发货。期待发货上图

  13. [...] 具体方法是从zww大师那里学来的,转来此处作为备份,以后为新的wordpress主题加上这个小工具。 [...]

  14. says:

    果然是名副其实得 widget啊,够简单!! :mrgreen:

    1. zwwooooo says:

      @追
      就是个widget。。。连插都懒得插,是插件。

  15. 这个已经在用了

  16. bbis says:

    俺的文章还少,等多了估计能用上

  17. 茶话汇 says:

    不错,z大,如何实现按周、月、年排序最热门(阅读量)的文章?

    1. zwwooooo says:

      @茶话汇
      这……其实自己看看参数也能,我就懒得折腾了

  18. 淘宝皇冠店铺导航 says:

    祝福朋友们身体健康,福运连连,欢笑天天,平安年年!

  19. 直接找分类,获取它下面的文章就行了啊

  20. 建站网 says:

    才一个月...在耐心等一下呗 :mrgreen:

发表评论

昵称 *

网址

B em del U Link Code Quote