这是一个朋友需要的小功能,扔这里,也许有人需要。
其实就是对最新文章代码加个分类过滤而已,做成小工具只是方便安排侧边栏的展示位置。
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. 然后进“外观 》小工具”,找到如下图,拖到右边的侧边栏小工具区域

3. 再然后,你懂的

就这样。
我的 Nexus 4 神马时候才能发货啊?Google!!!都一个月了!!!
zww
赏
oooo
or
- 本文标题:WP Widget: 带分类过滤功能的最新文章
- 本文链接:https://zww.me/archives/25736
- 发布时间:2012年12月27日 18:53
- 版权声明:除非注明,文章均为 zwwooooo 原创,转载请以链接形式标明本文地址!

沙發不解釋…
@小闇
@野蛮人
我接上:不回复也没关系。好顺~
@zwwooooo

不行不行,既然你佔了我家文章的沙發,禮尚往來
板凳。不解释。。。
前排。。。。。
靠背~~~
此博客体验很舒服,呵呵
折腾的大牛! 佩服佩服! 最近我要写学校的网站和小型系统 快愁死了!
@所谓刚子
有木有工资啊
@zwwooooo
有啊,就是没人做!找人都找不到!
@所谓刚子
有就别抱怨了,哈
还算前排吧?插入一个
调用某一分类的文章,貌似这个不错
这个太棒了,下次做主题就用上。以前还要用postview插件神马的,如今不用啦。
这个以前我也弄过,只不过需要自己输入分类的ID
@大发
懂得自己写就行了
路过,不解释
哈哈 原来还没发货。期待发货上图
果然是名副其实得 widget啊,够简单!!
@追
就是个widget。。。连插都懒得插,是插件。
这个已经在用了
俺的文章还少,等多了估计能用上
不错,z大,如何实现按周、月、年排序最热门(阅读量)的文章?
@茶话汇
这……其实自己看看参数也能,我就懒得折腾了
祝福朋友们身体健康,福运连连,欢笑天天,平安年年!
直接找分类,获取它下面的文章就行了啊
才一个月...在耐心等一下呗
装了差不多20几个插件了,我自己都不认识它们了!
@我的名字叫麒
20多个还可以接受,我见过有人插了50多个,囧~