There are many Recent post wordpress are available to show most recent posts. Blog Recent Posts widget can be added to sidebar or any other place.
These wordpress recent posts widgets give more exposure to your content and better conversion rate. Today let us see how to write our own recent posts widget that can be used in sidebar, pages or other places. Unlike WordPress plugin you don’t have to install any plugin. I is important to remember that the more number of plugins you use the loading time of your website will increase. So it is not advisable to use plugins for all purposes.
Today we will create a very simple recent post widget code that you can embed any where. This will help you to avoid using any plugins to display the Most recent posts. Our piece of code will display the most recent posts with Title, Author and number of comments.
As we discussed in earlier post ,usually wordpress won’t allow you to include PHP code in pots, page or sidebar. Read how to Insert, Execute, Embed or Include PHP Code in WordPress Post, Page or Sidebar. Once you have setup this you can add the below widget code where ever you want. Be it in sidebar , page , post or even footer.
Create your own recent posts widget
There are many useful functions provided by wordpress to access all your WordPress related data. They are all powerful but you can use it to tweak your WordPress theme of sidebar to make it more feature rich.
In the below code we are iterating through the list of posts which is returned using the function wp_get_recent_posts(). Then we retrieve each of the post details using different wordpress function inside a “for” loop. We are using a <div> as the container for the most recent osts. We have applied some CSS styles . You can move these style classes to your theme’s style.css file. You can change the CSS to suite your theme or requirement. The code is given below.
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>Most Recent Posts</h2> <ul style=""> <?php $recent_posts = wp_get_recent_posts(); foreach( $recent_posts as $post ){ $id = $post["ID"]; echo '<li><span class="removed_link" title="' . get_permalink($post[">' . $post["post_title"].'</span> <br/> <ol style=" font-size: 10px; color:#CC0000; text-decoration: none;">Author :' . get_the_author() . ' (Total Comments: '. wp_count_comments($id)->total_comments . ')</ol></li> '; } ?> </ul> </div> |
Before you go, subscribe to get latest technology articles right in your mailbox!.