Family Encyclopedia >> Electronics

How to limit the number of months of archive displayed in WordPress

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

How to limit the number of months of archive displayed in WordPress

Video Walkthrough

Subscribe to WPBeginner

If you don't like the video or need more instructions, continue reading.

Method 1. Limit the number of months of archive with the plugin

This method is easier and is recommended for all users.

The first thing you need to do is install and activate the Collapsible Files plugin. For more details, check out our step-by-step guide on how to install a WordPress plugin.

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

How to limit the number of months of archive displayed in WordPress

The widget menu will expand to show your settings.

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

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

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

How to limit the number of months of archive displayed in WordPress

Method 2. Replace default files with compact files

This method provides a cleaner alternative to the default Files widget by displaying them in a compact and presentable way.

First, you need to install and activate the compact files plugin. For more details, check out 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 Files' widget to your sidebar.

How to limit the number of months of archive displayed in WordPress

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

Don't forget to click the Save button to store your widget settings.

You can now visit their website to see the compact files in action.

How to limit the number of months of archive displayed in WordPress

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

Method 3. Manually limit the number of months of archive in WordPress

This method requires you to add code to your WordPress theme files. If you haven't done so before, check out our guide on how to copy and paste code in WordPress.

You will need to add the following code to the functions.php file of your theme or a site-specific plugin.

 // Función para obtener la lista de archivos con la función de meses limitados wpb_limit_archives () $ my_archives = wp_get_archives (array ('type' => 'Monthly', 'limit' => 6, 'echo' => 0)); devuelve $ my_archives; // Crear un shortcode add_shortcode ('wpb_custom_archives', 'wpb_limit_archives'); // Habilitar la ejecución de shortcode en el widget de texto add_filter ('widget_text', 'do_shortcode'); 

This code retrieves the list of files and limits it to the last 6 months only. Then create a shortcode and enable the shortcode on text widgets.

Now you can go to Appearance »Widgets page and add a 'Text' widget to your sidebar. Switch to text mode and add your shortcode like so:

 
    [wpb_custom_archives]

Don't forget to save your widget settings.

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

That's it, we hope this article helped you learn how to limit the number of months of archives displayed in WordPress. You may also want to see our list of these most useful WordPress function file hacks.

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