wordpress列出所有分类目录下一定数量的文章

  1. 不影响主LOOP, 分页正常工作
  2. 限制每个分类下文章数量
  3. 列出所有分类, 不用指定
<ul> 

	<?php
	/**
	 * wordpress list limited number of posts group by all categories
	 * without destroying the main hook
	 * coded by xiaohudie
	 * i promised i'd use English to annotate my code, and now i did it
	 * 2013-05-08
	 */
	global $post;
	$reset_post = $post;
	$cat =get_categories();
	foreach($cat as $category) :
		$args=array(
			'numberposts' => 5, // limit number
			//'ignore_sticky_posts'=>1,// defult:0 not ignore
			'category__in' => array($category->term_id) //use category id to hook the category parameters
		);
		$xhdpost = get_posts( $args );
		echo '<h3> Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </h3> ';
		foreach ( $xhdpost as $post ) : setup_postdata($post); ?>
			<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
	<?php
	endforeach; 
	endforeach;
	$post = $reset_post; // most important step to reset after postlists with offset 
	?>

</ul>

Demo:
20130508132438

16 Comments

Name *

E-mail *

Website

  1. hushan.me

    不错哦