How to Limit the Archive Menu List in WordPress

by Vincy. Last modified on July 13th, 2022.

In WordPress blog, the archive menu list will contain all the posts and generally as a chronological list. By clicking the date from the archive menu, we can see the list of posts published on the selected date.

The wp_get_archives() function is used to display the date-based archives index in our WordPress template. By default, the archives menu will contain month-based archives and it displays all archives.

If you have been posting for years, then the archives menu list will be too huge in length in your WordPress setup. In this WordPress post, we are going to customize the archives menu list by limiting its length. We set explicit limits as the parameters of the wp_get_archives() to get the limited number of archives.

I have implemented this by using WordPress theme’s functions.php. You can also create your own plugins to show customized archives index in WordPress template.

I have created WordPress shortcode in the functions.php file by using add_shortcode() hook. This shortcode will be used later in a suitable place, to embed the customized archives menu in the template.


WordPress Custom Code to Limit Archives Menu

Add this code to your theme’s functions.php to create a shortcode custom_archives_menu. This shortcode is hooked with the custom function named get_custom_archives_menu().

In this function, I have set the explicit limit and other arguments to the wp_get_archives()  to override the defaults. This function will return the archive menu HTML with the limited length as specified. Then, I used this shortcode in the sidebar.php to embed the archive menu HTML.



// Custom Function to get archives list HTML with limited years
function get_custom_archives_menu() { 
$archive_menu_list = wp_get_archives(array(
return $archive_menu_list; 
// Add shortcode
add_shortcode('custom_archives_menu', 'get_custom_archives_menu'); 



Shortcode Usage in WordPress Templates

After creating the shortcode hook to get the archives menu, we have to use it in the WordPress inbuilt or custom template file. In this section, I have specified the shortcode usage methods to show the archives menu list in the WordPress website.

As we specify only the type and limit params, the other parameters are taken by the wp_get_archives() function default. So, as per the default echo and format params, the custom function will show last 3 years archives menu in the form of HTML list <li>.

By adding this code in your theme files, an unordered list of archive menu with be displayed with limited length.

<?php do_shortcode("[custom_archives_menu]"); ?>
Written by Vincy, a web developer with 15+ years of experience and a Masters degree in Computer Science. She specializes in building modern, lightweight websites using PHP, JavaScript, React, and related technologies. Phppot helps you in mastering web development through over a decade of publishing quality tutorials.

Leave a Reply

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

↑ Back to Top

Share this page