Display Posts in the Sidebar on Specific Category Only – WordPress

Advertisement

Last few days, a friend of mine ask me to create wordpress tutorial to display posts in the sidebar on specific category only and I want to share you how easy to make it in wordpress using shortcode, you know already shortcode right? Well, you should, shortcode is a simple set of functions for creating macro codes for use in post content.

Ok, let’s start.

In this tutorial we’re going to use get_posts() functions.

get_posts()

This is a simple function for creating multiple loops. It retrieves a list of latest posts or posts matching criteria.

Syntax


<?php $posts_array = get_posts( $args ); ?>

Parameters

This functions use same parameters as WP_Query does, see below a few of those.

  • numberposts: The number of post to return.
  • category: The category ID where to get the list of posts.
  • orderby: Post field used to sort posts.
  • order: How to sort $orderby
    • ASC
    • DESC
  • include: Post ID to include separated by comma.
  • exclude: Post ID to exlode separated by comma.
  • post_type: Post type to return, default ‘post’.
  • post_status: Post status to return.
    • hold
    • approve
    • spam
    • trash

Note: The category parameter needs to be the ID of the category, and not the category name.

Ok, let’s start coding

Open functions.php file in your current theme or if you don’t have one, please create functions.php file and copy and paste the code below.


// add our shortcode
add_shortcode('ryans_get_posts', 'ryans_get_posts');

function ryans_get_posts( $atts ) {
	global $post;
	$str = '';

	extract( shortcode_atts( array(
		'numberposts' => '5',
		'offset' => '',
		'category' => '',
		'order' => 'DESC',
		'include' => '',
		'exclude' => '',
		'meta_key' => '',
		'meta_value' => '',
		'post_type' => 'post',
		'post_mime_type' => '',
		'post_parent' => '',
		'post_status' => 'publish',
	), $atts ) );

	$args = array(
		'numberposts'     => $numberposts,
		'offset'          => $offset,
		'category'        => $category,
		'orderby'         => $orderby,
		'order'           => $order,
		'include'         => $include,
		'exclude'         => $exclude,
		'meta_key'        => $meta_key,
		'meta_value'      => $meta_value,
		'post_type'       => $post_type,
		'post_mime_type'  => $post_mime_type,
		'post_parent'     => $post_parent,
		'post_status'     => $post_status
	);
	$postslist = get_posts( $args );

	$str = '<div class="recent-article-box">';
		$str .= '<ul class="recent-article">';
			foreach( $postslist as $post ) :
				setup_postdata($post);
				$str .= '<li><a href="'. get_permalink( $post->ID ) .'" title="'. $post->post_title .'">'. $post->post_title .'</a></li>';
			endforeach;
		$str .= '</ul>';
	$str .= '</div>';
	wp_reset_query();

	return $str;
}

We use wp_reset_query() functions to destroy the previous query used on a custom loop.

How to use the shortcode?

Put [ryans_get_posts] in your post, pages or text widget.

That displays all posts in all categories, to limit post display on specific category only, add category parameter.


[ryans_get_posts category="5"]
// where 5 is the category ID

To limit post display use numberposts parameter.


[ryans_get_posts numberposts="10" category="5"]
// where 10 is the number of posts to display.

To add more parameters use parameters I’ve listed above.

Troubleshooting

If the shortcode didn’t work in the text widget, don’t worry folks I’ve a solution on it.

Add this short snippet in functions.php of your theme.


add_filter( 'widget_text', 'do_shortcode' );

Don’t want to get your hands dirty?

Not a problem folks here is the plugin from other developer Category Posts Widget.

That’s it happy coding.

Advertisement