I’m creating a custom walker for my nav menu. The goal is to split the menu in to columns like so:
I can easily determine where I am in the list by looking at $item->menu_order
. What I don’t know is how many items are in the menu total.
How can I figure that out from inside the walker?
Updated:
I realize there are several other ways to create columns. All have their drawbacks, however:
-
CSS Floats. As suggested below by @sagive, this could be accomplished by floating all of the list elements. The problem is that the list changes order. Instead of going down the first column, then wrapping over to the second, the list would go across all four columns on the first row, then across all four columns in the second row. This messes up the order.
-
CSS Columns. This is almost the perfect solution. It will give me exactly what I want — except that IE doesn’t it at all. Even on IE 9. That makes it unusable for a mainstream web site.
-
jQuery. With some client-side javascript I could hack up the list and make it layout the way I want. That’s messy though, and it breaks for people who have js disabled.
-
PHP. This is the method I’m asking about in this question. I break the list in to four separate unordered lists, dump each out in html, then use CSS to float the lists to make four distinct columns. It may be tricky to produce in PHP, but the result looks the same for all browsers. That makes it the best option I believe.
Solution:
For those who may follow, here’s the code I finally used to get the answer:
// The menu handle from the register_nav_menu statement in functions.php
$theme_location = 'my-menu-handle';
$theme_locations = get_nav_menu_locations();
$menu_obj = get_term( $theme_locations[$theme_location], 'nav_menu' );
// Echo count of items in menu
echo $menu_obj->count;