You can see blogs and sites with a magazine style WordPress theme displays posts from a specific category. You may want to display posts from a specific category in the home page or any other page. There may be plugins available to achieve the same. But the more the number of plugins you use to make you blog feature rich the more heavy it becomes. As a result the site performance may go down. The page load time may increase. To avoid this problem you can write your own code to display the most recent posts from a category where you want to display it.
Related Post:

  • 60+ Most Useful WordPress Tips, Tricks, Tutorial and Hacks
  • WordPress Tips: Create Your Own WordPress Recent Posts Widget
  • WordPress Tips: Create Your Own WordPress Random Posts Widget

In this post we will make use of the WordPress function “get_posts” to display the posts from a specific category.Your won wordpress-category-posts widget

Display posts from a specific category without using plugins

Today we will create a simple recent category post widget code that you can embed wherever you want. This helps you to display most recent posts from any category without using plugins. One point to remember here is that usually wordpress won’t allow you to include PHP code in pots, page or sidebar.
You can follow the steps mentioned in the post how to Insert, Execute, Embed or Include PHP Code in WordPress Post, Page or Sidebar to make wordpress code executable any where. Once you have setup this you can add the below widget code any where including sidebar , page , post or even footer.

Create Your own Category Post Widget

We make use of the WordPress function “get_posts()” which accepts a number of parameters. It retrieves a list of latest posts or posts matching the parameter criteria.
The method “query_posts()” also can be used for the same purpose but the method causes additional SQL queries. Also it affects Conditional Tags after it’s called.
The below code displays the recent 5 posts from the category wordpress tutorials with id 12345.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<div style="background: none repeat scroll 0 0 ##ffffff;
    border: 0.2em solid #1177AA;
    color: #222222;
    font-family: HelveticaNeue,Arial,Helvetica,sans-serif;
    font-size: 12px;
    font-weight: 700;
    line-height: 20px;
    padding: 0 3px 0 8px;
    text-decoration: none;
    vertical-align: baseline;
    width: 400px;;">
<h2 class="note">Most Recent Posts</h2>
<ul style="">
<?php
$args = array( 'numberposts' => 5, 'category' => 12345 );
$cat_posts = get_posts( $args );
foreach( $cat_posts as $post ) :  setup_postdata($post);  ?>
	<li><a style=" font-size: 12px; font-weight:bold; text-decoration: none;" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
	<ol style=" font-size: 10px; font-weight:bold; color:#CC0000; text-transform: uppercase;"><?php the_date(); ?> | Author : <?php the_author(); ?>  |  <?php comments_number('no responses','one response','% responses'); ?></ol>
	</li>
<?php endforeach; ?>
</ul>
</div>

In the above code you need to change the value of category id (‘category’ => 12) with the required category of your blog. You can also change the number of posts to any number of values(‘numberposts’ => 5). Also you can change the default style class based on your theme and preferences. The above code will display the chosen category posts as below.Your won wordpress-category-posts widget

Binu GeorgeWordPress Tipshow-to guides,productivity,Top 10,wordpress category page,wordpress get posts,wordpress post category,wordpress show posts,wordpress tipsYou can see blogs and sites with a magazine style WordPress theme displays posts from a specific category. You may want to display posts from a specific category in the home page or any other page. There may be plugins available to achieve the same. But the more the...Tech-Knowledge that helps