How to Limit the Number of Archive Months Displayed in WordPress

Are you looking for ways to limit the number of archive months displayed in WordPress? Bloggers who have been blogging for a long time now might notice their archives list becoming too long. Well, there are few methods you can use to limit the number of archive months displayed in WordPress. And today, at DevotePress, we will be showing you how to do that with and without plugins. So, without any further ado, let’s get into the article.

Limit the number of Archive Months with Plugin

This method is quite easy and is recommended for everyone, including the beginners. Firstly, you will need to install and activate the Collapsing Archives plugin.

After activating the plugin, you need to visit Appearance > Widgets from your website’s dashboard. After that, add the Collapsing Archives widget to your sidebar.

collapsearchive

The widget menu will now expand to show its settings. The Collapsing Archives plugin uses JavaScript to collapse your archive links into collapsible yearly links. Your users will be able to click on years to expand them to view monthly archives. You can also make monthly archives collapsible and allow your users to see the post titles underneath.

Now, review the widget settings to suit your needs and after you’re done, click on the ‘Save’ button.

Your settings are now saved and you can visit your website to see the widget in action.

archivescollapsed

Having said that, now let’s see how you can limit the number of archive months in WordPress without a plugin.

Limit the Number of Archive Months Manually

This method might be a little complicated for the beginners as it requires you to add code to your WordPress theme files.

You will have to add the following code to your theme’s functions.php file or a site-specific plugin.

 // Function to get archives list with limited months
function wpb_limit_archives() {

$my_archives = wp_get_archives(array(
    'type'=>'monthly',
    'limit'=>6,
    'echo'=>0
));

return $my_archives;

}

// Create a shortcode
add_shortcode('wpb_custom_archives', 'wpb_limit_archives');

// Enable shortcode execution in text widget
add_filter('widget_text', 'do_shortcode'); 

The code will fetch the archives list and limit it to past 6 months only. Then, it will create a shortcode and enable that shortcode in text widgets.

Now, you can go to Appearance > Widgets page and add a ‘Text’ widget to your sidebar. You need to switch to the text mode and add your shortcode as shown below:

 
<ul>
[wpb_custom_archives]
</ul>

 

Hit the ‘Save’ button after you’re done with the shortcode. Visit your website to see your listed archives in action.

That’s all you need to do. If you’re a beginner and don’t want to mess around with coding, then we suggest you use the first method, that is limit the number of archives months displayed in WordPress with a plugin. And if you’re familiar with coding, you can easily use the second one. Either way, your custom archives list will work elegantly on your website.

Do you have anything to add, share or ask? Let us know about them in the comments.

 

Reference: WP Beginner

Leave a Reply

Your email address will not be published. Required fields are marked *