Get List of Posts By Category Like wp_get_archives(), With Date

This function for WordPress gets an HTML list of posts, just like wp_get_archives(), but by specified category. The output is an HTML unordered list of links with each post title as the anchor text, and the date of the post next to it.

PHP

/**
 * Get an HTML list of posts, like wp_get_archives(), but for a specified category
 */
function isa_get_archives_by_cat( $category_slug = '' ) {
	$args = array(
		'category_name'		=> $category_slug,
		'post_type'			=> 'post',
		'posts_per_page'	=> -1,
		'nopaging'			=> true,
		'no_found_rows'		=> true
	);
	$listings = new WP_Query( $args );
	if ( ! $listings->have_posts() ) return;
	$items = '';
	while ( $listings->have_posts() ): $listings->the_post(); global $post;

		// link with title as anchor text (with date in parenthesis next to it)

		$items .= sprintf( '<li class="listing-item"><a href="%s">%s</a> <span class="date">%s</span></li>',
				get_permalink(), get_the_title(), get_the_date( '(n/j/Y)' ) );

	endwhile;
	wp_reset_postdata();
	return '<ul>' . $items . '</ul>';
}

Usage

Use the function to get an HTML list of post links for the category that you want. Replace MY_CAT_SLUG in the following line to your own category slug.

isa_get_archives_by_cat('MY_CAT_SLUG');

See more: ,

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]