How to show specific category posts on a page

Spread the Knowledge

If you want to show specific category posts on a page, Or want to show different categories post on different pages, then you are in right place.


To show recent posts from a specific category or categories on any page, Go to your theme folder, and put the below code at the end of functions.php file.

<?php function w3b_recents_post() { 
   ob_start(); 
   $args = array(); 
   $args['cat'] = 1; 
   $args['posts_per_page'] = 5; 
   $args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; 
   $the_query = new WP_Query( $args ); 
   if ( $the_query->have_posts() ) {
		echo '<div class="w3b_cat_posts">';
		while ( $the_query->have_posts() ) {
			$the_query->the_post();
			echo '<div class="w3b_post">';
			echo '<h3 class="post_title"><a href="'.get_the_permalink().'" rel="bookmark">'.get_the_title().'</a></h3>';
			echo '<div class="entry_content">';
			the_excerpt();
			echo '</div>';
			echo '</div>';
		}
		echo '</div>';
		/* Restore original Post Data */
		wp_reset_postdata();
	} else {
		echo "No Post Found...";
	}
	
	return ob_get_clean();
}

add_shortcode( 'w3b_recents_post', 'w3b_recents_post' );

Now we will discuss the above code line by line…

  1. We have created a function with function name w3b_recents_post.
  2. Ignore ob_start function. We will discuss it later.
  3. Line 3 to 6 we have defined parameters for the query function, for more parameters read this.
  4. Line 7, we created an object of class WP_Query.  It will execute a Mysql query.
  5. Line 8, We check if the MySQL query returned any posts According to the given parameters. If yes then we printed a div container for the post.
  6. Line 10, We run while loop for all the returned posts, And assign all post data to global $post variable by calling '$the_query->the_post()' function.
  7. Inside the while loop, we print some HTML post title in an h3 tag by calling, get_the_title().
  8. And then call ‘the_excerpt()‘ To show post excerpt, If you want to show full post then replace this function with the_content()
  9.  After ‘While loop’ we must call, wp_reset_postdata() Which will restore the original post data.
  10. In the ‘Else’part of the ‘if condition’, if no post returned, we will print some message like “No Post found….”.
  11.   At the end of the function, we have to call ‘return ob_get_clean().
  12. Use this shortcode [w3b_recents_post] in any page or post to show recent post from a specific category.

Ob_start And ob_get_clean

WordPress shortcode need to return value, But as you can see we have a lot of HTML. The first way is to assign all HTML to a variable and then return at the end. but it will look messy. So we printed all HTML using “echo” statement. And used ob_Start to capture the output. it means nothing will be printed.

The function ob_get_clean get the output buffer and which will be returned by the return statement. and then it will delete output buffer.

 

Hope this article will help you in showing recent posts from a specific category. If got any issues when using this code, Please let us know in the below comment section. We will try our best to help you.

Spread the Knowledge

Leave a Reply