How to Limit the Amount of Menu Items in WordPress

Home » Snippets » How to Limit the Amount of Menu Items in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

When managing a website, it’s important to keep navigation simple and intuitive for your users. An overcrowded menu can frustrate users, cause confusion, and ultimately lead to a poor user experience. Therefore, you may find yourself in need of limiting the amount of menu items on your WordPress site. In this article, we’ll provide a thorough guide on how to effectively do this, ensuring a seamless navigation experience for your users.

					function wpturbo_limit_menu_items($items, $args)
{
    if ($args->theme_location == 'primary') {
        return array_slice($items, 0, 5);
    }
    
    return $items;
}
add_filter('wp_nav_menu_objects', 'wpturbo_limit_menu_items', 10, 2);
				

This code snippet essentially limits the number of menu items that are displayed in a specific WordPress theme location.

The first line of the code, function wpturbo_limit_menu_items($items, $args), specifies a new function called wpturbo_limit_menu_items(). The $items parameter represents the array of menu items added in the WP Dashboard panel, while $args contains theme arguments, such as the menu’s theme location, the container type for the menu, and more.

Next, we get to the body of the function, where the cutting action takes place:

if ($args->theme_location == 'primary') {
        return array_slice($items, 0, 5);
    }

In this part, an if-statement checks whether the $args->theme_location is equal to 'primary'. The theme_location argument refers to the registered location of the theme by the developer in the functions.php file.

If the theme location of the menu being processed is ‘primary’, then the given menu items ($items) are sliced via PHP’s array_slice() function. The array_slice() function takes in three arguments – the array, the offset (which is 0 in this case, meaning we’re starting from the beginning), and the length (or the number of menu items you want to include) – here set to 5.

Therefore, in essence, this snipped will only return the first five menu items from the beginning of a ‘primary’ menu.

In the case where the theme_location is not set to ‘primary’ or is not provided, the filter will resort back to its original state and return the $items as usual:

return $items;

Finally, the function wpturbo_limit_menu_items() is added to the wp_nav_menu_objects filter in WordPress, which is responsible for filtering the sorted menu items:

add_filter('wp_nav_menu_objects', 'wpturbo_limit_menu_items', 10, 2);

The add_filter() function assigns a callback function to a specific filter action, (‘wp_nav_menu_objects’ in this case). The 10 is the priority (in what order it should be applied compared to other callbacks, lower number means earlier execution), and 2 is the number of arguments passed (the menu items and args).

This will trigger and feed the $items and $args variables in our wpturbo_limit_menu_items() function whenever the wp_nav_menu_objects filter is invoked, dynamically determining whether to truncate the menu or not.

Register an account to save your snippets or go Pro to get more features.