How can I set a limit to the top level menu items in wp_nav_menu
?
Example:
I have this menu in my admin dashboard:
I would like to count ONLY the top level items and display a limited number of them, let’s say 5.
So no matter if you’re in the admin dashboard and you have 100’s of top levels, it should only display 5.
The sub-pages should be displayed as well, normally, without any limit.
I searched for an answer here and on the web and I saw this answer but big problem is that this solution counts top and sub-pages altogether and I need to count only top pages.
I would like to see some solution with a custom walker class if possible or PHP in general and not with CSS and jQuery tricks and hacks.
According to the codex, you just need to use
depth
parameter to display only top-level pages :For more reference see this.
You could also fix both of your problem by using
[wp_get_nav_menu_items][2]
and then using a custom loop to parse only first and top-level pages.EDIT***
I did spent some time starting something to help, unfortunately I cannot finish it right now, and maybe could make it more elegant, but that could be a start :
you can try something like:
Did not had the chance tot test this.
For reference, Sormano was on the right path, but the filter passes a string. The correct version would be:
Obviously, this only works when you also use
depth=1
. But it is a simpler solution to creating a customer walker if all you need is a simple solution.