Create WordPress Widget To Display Recent Posts From Category
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.
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.
-
http://theaterbalcony.wordpress.com TheaterBalcony
-
Binu George
-
-
http://www.plumeriawebdesign.com Elke Hinze
Join Globinch on Social Media
Follow Globinch
Popular Posts
- Turn your Windows 7 Computer Into a Personal Wi-Fi Hotspot 20 comment(s) | 26,500 view(s)
- Search Engines for File Sharing Sites : Rapidshare Search, Megaupload and More 2 comment(s) | 18,457 view(s)
- Google Translate Now supports Bengali, Gujarati, Kannada, Tamil and Telugu 2 comment(s) | 17,346 view(s)
- Best PDF OCR Tools to Convert Scanned images to Text / Word Documents 4 comment(s) | 13,642 view(s)
- Google Top Searches Today – Hot Google Search Words today 8 comment(s) | 13,504 view(s)
- Globinch Search 0 comment(s) | 10,389 view(s)
- Access Blocked sites, Unblock Restricted Websites 16 comment(s) | 9,453 view(s)
- Password Revealer – How To Reveal- Show Password Behind Asterisks? 1 comment(s) | 8,517 view(s)
- Play Angry Birds Game Free and Offline in Chrome Browser 1 comment(s) | 8,244 view(s)
- Free Online OCR Software to Convert Images Into Searchable PDF, Doc, HTML or Text 2 comment(s) | 8,083 view(s)
Recent Articles
- How to Use Wi-Fi the Secure Way
- 5 Apps to Boost Android Performance
- Building a Smarter Site Using a WordPress Platform
- Samsung Galaxy S IV Smartphone :Summary of All the Radical Innovations
- 5 Killer Tips to Increase Traffic to Your Blog
- Domain Parking to Make Genuine Money Online
- SEO Is Not Dead, But You Need to Upgrade The Process
- A Complete SEO Guide on Using Social Media for Better SERPs
- When Should You Use Geotargeting or Setting Geographic Target in Google Webmaster Tools?
- How To Optimize Your Google+ Page And Profile For Search Engines








