Wordpress Building simple lists using get_posts()

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 = '<h1><a href="'. get_pretty_category_link($category) .'">'. get_cat_name($category) .'</a></h1>';
	$html .= '<ul class="simple_list">';
    foreach($posts as $post) :
		setup_postdata($post);
		$html .= '<li>';
		$html .= '<a title="'. the_title('','',false) .'" href="'. get_permalink() .'" rel="bookmark">'. the_title('','',false) .'</a>';
		$html .= '</li>';
    endforeach;
 
	$html .= '</ul>';
 
	return $html;
}

Use

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

Post a Comment



Follow Me