How to limit the number of archive months shown in WordPress

A post archive is a valuable feature included in many WordPress themes. Archives usually display a link in the sidebar for each month that includes at least one published post. This helps you and your readers quickly find posts from specific months and years. However, if you’ve been publishing posts for a long time, the displayed archives can get challenging to use and unsightly.

In this article, you’ll learn how to limit the number of archive months shown in the sidebar with plugins or by manually editing your theme’s functions.php file.

Using plugins to limit the archive

There are two popular plugins you can use to limit how many months are shown in the WordPress archive:

Using the Compact Archives plugin

The Compact Archives plugin displays archive months in a compact, easy to read, layout. The easiest way to use Compact Archives is with the built-in sidebar widget, which we’ll explain in this article, but you can also add the compact_archive template tag to your template or insert the compact_archive shortcode in any post or page.

Log in to your WordPress dashboard and go to Plugins > Add New. Type compact archives in the search field. In the search results, find Compact Archives and click Install Now.

When the plugin is installed, click Activate.

Next, go to Appearance > Widgets. Drag the Compact Archives Widget from Available Widgets to Blog Sidebar.

Enter a title for the archives, select the style, and then click Save to transform the text.

The compact archives are now displayed in your site’s sidebar.

Using the Collapsing Archives plugin

Another WordPress plugin you can use to limit the archive is Collapsing Archives, which uses JavaScript to enable collapsing the sidebar archive by month or year.

Log in to your WordPress dashboard and go to Plugins > Add New. Type collapsing archives in the search field. In the search results, find Collapsing Archives and click Install Now.

When the plugin is installed, click Activate.

Next, go to Appearance > Widgets. Drag the Collapsing Archives widget from Available Widgets to Blog Sidebar.

There are several settings available. Select the settings that are suitable for your site, then click Save.

The collapsing archives are now displayed in your site’s sidebar.

Editing functions.php to limit the archive

This method of limiting the archive requires editing your theme’s functions.php file, then adding a custom text widget to the sidebar.

Log in to your WordPress dashboard, go to Appearance > Editor, and open Theme Functions (functions.php). Add the following lines of code to functions.php, then click Update File.

// Function to limit archive months
function wp_limit_archives() { 
 
$my_archives = wp_get_archives(array(
    'type'=>'monthly', 
    'limit'=>6,
    'echo'=>0
));     
return $my_archives;  
} 
 
// Create the shortcode
add_shortcode('wp_custom_archives', 'wp_limit_archives'); 
 
// Enable the shortcode in the custom text widget
add_filter('widget_text', 'do_shortcode'); 


Next, go to Appearance > Widgets. In Available Widgets, click Text and select Blog Sidebar. Click Add Widget.

Enter a title for the sidebar widget. Click the Text tab on the editor and enter the following code, then click Save.

<ul>
[wp_custom_archives]
</ul>

The result of your custom text widget is now displayed in your site’s sidebar.

If you need help with your HostPapa account, please open a support ticket from your dashboard.

Related Articles

Get online with our affordable web hosting

Get online with our affordable web hosting

Learn more now
HostPapa Mustache