WordPress Building simple lists using get_posts()

Apr 11, 2009   //   by Michael D. Irizarry   //   PHP, Wordpress  //  1 Comment

It’s really simple to build lists using the get_posts() method. Here’s an example of getting all the posts from a single category.

PHP

// Simple Lists
function get_simple_list($category) {

	global $post;
	$posts = get_posts('numberposts=5&category='.$category);

	$html = '

'. get_cat_name($category) .'

'; $html .= '
    '; foreach($posts as $post) : setup_postdata($post); $html .= '
  • '; $html .= ''. the_title('','',false) .''; $html .= '
  • '; endforeach; $html .= '
'; return $html; }

Use

_e(get_simple_list(5)); // Where 5 is the category ID

1 Comment

Leave a comment