WordPress enables a blogger to display their archive by months without any limit placed on the number of archives that can be displayed. Say, you are a blogger and have been running your blog for a long time now, you would have noticed that your blog’s archives list would have become too long which makes it very difficult for a site visitor to explore or browse your old posts.
So, let us learn in this article, that how do we limit the number of archive months and improve visitor experience.
What are archives ?
Search engines like Google and Bing always try to display the latest information or posts on your site. In case, your blog is not very established, then in that case, the content will be displayed on the basis of date. Therefore, archiving is extremely beneficial from SEO (Search Engine Optimization) perspective since it ensures long-term search engine visibility of your blog’s content.
Now, let us understand how to limit the number of archive months.
There are different methods to Limit the Number of Archive Months Displayed in WordPress. I have explained both the methods in detail below:
- By using FREE plugin
- Without using any plugin.
Method-1: Limit the Number of Archive Months with a FREE Plugin.
Below are the steps:
1. First, you need to install and activate the Compact Archives plugin.
2. Upon activation, go to Appearance » Widgets page.
Method-2 Manually Limit the Number of Archive Months in WordPress.
This method requires you to add code to your WordPress theme files in functions.php file.
Always remember, whenever you do any changes to any wordpress core file, do take a backup of your wordpress site, so that you can restore it back in case anything goes wrong.
1. You will need to add the following code
// Function to retrieve the list of archives by limiting it to past 12 months
function month_limit_archives() {
$my_archives = wp_get_archives(array(
‘type’=>’monthly’,
‘limit’=>12,
‘echo’=>0
));
return $my_archives;
}
You can specify the type of archive to retrieve with options being ‘daily’, ‘weekly’, ‘monthly’, ‘yearly’, ‘postbypost’, or ‘alpha’. For eg, if you want to fetch the daily archives of last 2 weeks so, you can specify type daily and limit 14 in the above code.
Now, let us create a shortcode and enable execution of that shortcode.
// Create a shortcode
add_shortcode(‘wpb_custom_archives’, ‘month_limit_archives’);
// Enable shortcode execution in text widget
add_filter(‘widget_text’, ‘do_shortcode’);
2. You can now go to Appearance » Widgets page.
3. Add a ‘Text’ widget to your sidebar. Switch to the text mode and add your shortcode as mentioned below and save your widget settings.
[wpb_custom_archives]
4. Visit your site to see your custom archives list in action.
So the above process illustrates how to manually add the code to limit the number of archive months on your site. You can also implement Method 1 if you are not very familiar with the code.
Would you please post your opinion or queries or feedback below.