How to Limit the Number of Archive Months Displayed in WordPress

Do you want to display the number of archive months displayed in WordPress? If you have been blogging for years, you will notice that your archives list will become too long. In this article, we will show you how to limit the number of archive months displayed in WordPress.

How to limit number of archive months in WordPress

Method 1. Limit Number of Archive Months with Plugin

This method is easier and it is recommended for all users.

First thing you need to do is install and activate the Collapsing Archives plugin. For more details, see our step by step guide on how to install a WordPress plugin.

Upon activation, you need to visit Appearance » Widgets page and add the Collapsing Archives widget to your sidebar.

Collapse archives

The widget menu will expand to show its settings.

The Collapsing Archives widget uses JavaScript to collapse your archive links into collapsible yearly links. Your users can click on years to expand them to view monthly archives. You can even make monthly archives collapsible and allow users to see post titles underneath.

Review the widget settings to suit your needs and then click on the Save button to store your settings.

You can now visit your website to see the widget in action.

Collapsing archives

Method 2. Replace Default Archives with Compact Archives

This method provides a cleaner alternative to the default archives widget by beautifully displaying them in a compact more presentable way.

First, you need to install and activate the Compact Archives plugin. For more details, see our step by step guide on how to install a WordPress plugin.

Upon activation, you need to visit Appearance » Widgets page and add the ‘Compact Archives’ widget to your sidebar.

Compact Archives settings

Compact Archives plugin comes in three styles. You can choose from block, initials, or numeric.

Don’t forget to click on the save button to store your widget settings.

You can now visit your website to see the compact archives in action.

Compact Archives preview

You can also use Compact Archives plugin to create a custom archives page on your site. For more details see our guide on how to create compact archives in WordPress

Method 3. Manually Limit Number of Archive Months in WordPress

This method requires you to add code to your WordPress theme files. If you haven’t done this before, then take a look at our guide on how to copy and paste code in WordPress.

You will need 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');

This code fetches the archives list and limits it to past 6 months only. It then creates a shortcode and enables shortcode in text widgets.

You can now go to Appearance » Widgets page and add a ‘Text’ widget to your sidebar. Switch to the text mode and add your shortcode like this:



    [wpb_custom_archives]

Don’t forget to save your widget settings.

You can now visit your website to see your custom archives list in action.

That’s all, we hope this article helped you learn how to limit the number of archive months displayed in WordPress. You may also want to see our list of these most useful tricks for the WordPress functions file.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

The post How to Limit the Number of Archive Months Displayed in WordPress appeared first on WPBeginner.



source http://www.wpbeginner.com/wp-tutorials/how-to-limit-the-number-of-archive-months-displayed-in-wordpress/