If you run a blog using WordPress, you may want to display the recent posts on the site to attract your readers to your latest posts and to get more page views.
Recent posts can be added to the sidebar or at the end of a post.
There are two different ways in which you can display recent posts on your site. Let us go through each one and you can choose the method that suits you best.
##Method 1: Using a Widget
###With the built-in Recent Posts Widget
WordPress has a default Recent Posts Widget that comes built-in. To add it to your blog, first, login into the WordPress Admin dashboard. Click on Appearance->Widgets from the left sidebar.
Drag the Recent Posts Widget to a sidebar of your theme.
Some themes might have the Recent Posts Widget already present in one of the sidebar areas. You can move it around to any other widget area present in your theme.
This widget has a few simple customization options. Change the title of the widget if you want. The default number of recent posts displayed is 5. You can change it to suit your needs. You can also choose to show data along with the recent posts list. Click on the Save button to store the settings.
This is the simplest form of incorporating Recent Posts on your blog.
Visit your site and take a look at any post on your blog. You should see the recent posts.
###Using a plugin for recent posts widget
Quite often, the default recent posts widget's functionality is just not sufficient. If you want more functionality for recent posts display, like showing thumbnails, or showing recent posts of a particular category, you need to use a plugin.
####Installing the plugin
There are several recent posts plugins available. Each of them has its own advantages and disadvantages. For the purpose of demonstration, install the Flexible Posts Widget Plugin.
To install this plugin, login into the WordPress Admin dashboard.
From the left sidebar, click on Plugins->Add New and search for Flexible Posts Widget. From the results, click on Install Now next to the Flexible Posts Widget plugin.
After successful installation of the plugin, click on Plugins->Installed Plugins and click on the Activate link below Flexible Posts Widget in the listing.
Click on Appearance->Widgets from the left sidebar.
Drag the Flexible Posts Widget to a sidebar of your theme.
This widget offers more flexibility in displaying your recent posts.
As in the case of the default widget, you can change the title.
The next section is interesting because it gives more options. The posts that need to be displayed in the recent posts listing can be customized in the Get Posts by section.
There are three tabs in this section. In the Post Type tab, you can choose if you want to list only posts (which is the default behaviour) or pages and media as well. Adding media to recent posts might be interesting for a media-rich blog, like a photography blog.
The second tab is titled Taxonomy & Term. Here the posts or pages can be chosen by a term in any taxonomy, say categories or tags or any custom taxonomy that you may have created. For creation of custom taxonomies for your blog, do take a look at this tutorial.
Once you choose a taxonomy from the dropdown box, the terms of the taxonomy are displayed in the Select terms: box below. Check on those terms that you want your filter to be based upon.
You can also specify by ID, which is the third tab option. This option is not useful under normal circumstances, but in case you need to, it is available,
If you have sticky posts (posts that are displayed on the front-page in spite of their publication date), do you want them to be ignored or included in your recent posts listing? You can exercise your choice with the next checkbox option.
Number of Posts to show option is the same as the default widget's feature.
Then next is another interesting feature, which is Number of Posts to skip. You need not list the posts from the latest one. You can choose to start he listing from an offset.
Usually recent posts are ordered by publication date. This plugin can list posts ordered based on various other parameters as well. For displaying recent posts, order by Publish Date. You can also list posts by Modified Date, Title, Menu Order (for Pages), Post ID, Author, Post Slug (the short url), Comment Count, Random and Post ID order (post ID order given in the post__in array), by picking the required option from the dropdown box for Order Posts by.
For each of these parameters, you can choose to list the posts in ascending or descending order, by picking it from the next dropdown box for Order.
Lastly, it looks good to display the thumbnails of the posts' images along with the link to the recent posts. Images can attract readers and make them curious about the post. The flip side is that the page load speed can go down because n (usually 5) more images are loaded.
If you decide to have thumbnails, check on the checkbox for Display thumbnails?
Lastly, there is an option to choose the template file to be used for display. Of the two options, Default and Widget, you can choose any one. This template file affects the appearance of the listing on your site.
After setting the options, click on Save. Now, go to your site, visit any post or page. You can see the Widget and the listing of the posts as per your needs.
It might look like this if you chose three posts, and did not want thumbnails to be displayed.
##Method 2: Through Code
The other option is to write a small piece of code and insert it at the right place in the theme files. Though slightly technical in nature, this is the most flexible method which will give you the exact result that you want without overloading your site with tons of unused features. This method also allows you to place the recent posts at any place that you want on your site.
For extracting the recent posts, firstly, you should query the WordPress database by constructing a query. Use the WP_Query class with the right set of arguments.
The complete set of arguments and the values the WP_Query can take can be referred to in the Codex.
For getting the last 6 recent posts of your site, here is the arguments array,
Now, you have to create a query object and loop over it.
have_posts()) : $the_query -> the_post(); ?>
Within this loop, you can extract what you need of the post, say title and link.
// Display the title of the post with the link to it
You can also try showing the thumbnails by including this piece of code. This piece of code takes care to check if there is any thumbnails associated with the post.
If you want the excerpt, add a call to this template tag.
You can also combine the three as below. Add a CSS float property so that the thumbnail image is to the left and the title and excerpt wrap around it.
Finally you have to reset the post data.
Combine all the pieces of code above and add a html unordered list to get the following code.
$the_query = new WP_Query( $args );
while ($the_query -> have_posts()) : $the_query -> the_post();
// Display the title of the post with the link to it ?>
For the twenty fifteen theme, open the file wp-content/themes/twentyfifteen/sidebar.php from your WordPress folder. Look for the widget area.
It is here, at the grey highlighted area that you have to insert this code.
Go to the site and see for yourself that the recent posts listing is present.
You will have to style the listing using CSS to make it appear more elegant.
You can also include this code after a post, instead of doing it in the widget area. For this purpose, you need to make changes in the wp-content/themes/twentyfifteen/single.php file which is used to structure the posts of your site.
You can explore and experiment on this.
I hope you learnt something useful in this tutorial. I also hope that you experiment with code and make wonderful things with your site.
(The tutorial is based on WordPress version 4.2.4, Twenty Fifteen theme version 1.2 and Flexible Posts Widget version 3.5.0.)
If you find your cPanel disk space filling up, or an email address has hit its disk space quota, cPanel has a helpful built in Email Disk Usage tool. This will provide you with a simple to understand breakdown of how much disk space each folder for a particular email...
Although WHM will normally automatically keep itself up to date, you may want to manually check for server updates / push through an update that is pending, or it may be that you have automatic updates switched off on your cPanel server. In this guide we will show you how...
You may sometimes need to manually adjust the PHP settings on your cPanel server – for example if a site is hitting the PHP memory, or file size upload limit. WHM allows you to quickly change the settings of any PHP version installed on the server when needed, using the...