I would like to display a custom 3.0 menu in my sidebar. I want the list to be in order of name, and the list to be sequential down the list of the left column and flow down, then into the right column.
I currently am using css and floating the li’s but the problem is the list goes back and forth from left to right column, rather than down the list sequentially.
I am using
<?php wp_nav_menu('menu=canon-camcorders'); ?> in my sidebar.php
Then, #sidebar ul#menu-canon-camcorders li.menu-item {width:89px; float:left; margin-right:18px;}
What I would like to do is count how many list items there are, then divide it into two and create two ul’s from that. My php abilities are sort of patched together without any formal training, so I am kind of lost. I know some jquery, but don’t know how to do this in either way.
I read this post: How to count nav menu items? But, I couldn’t figure it out for my needs.
Any help is greatly appreciated.
Thank you, Larry
You’ll need to register your own custom walker, but it’s pretty simple as you only need to have one function(
start_el
) inside the custom walker.We’ll have to add the code from the thread you referenced to work out how many items there are, then work out what half is and end the current list when that value is met and start another.
I’m not always good at explaining these things, so first take a look at this walker i wrote for you. It’s based off the default nav menu walker, so nothing special going, except a few lines here and there that work out when to create a new list.
I create two variables inside the class, the first
$current_menu
is going to hold the menu object, and we don’t want to be fetching that we every iteration of a menu item, so i give that a default null value and fetch the menu object when that variable isn’t set, this ensures it only makes the call once. The second variable$break_point
will hold the break point, ie. the value we’ll use to determine when to end the current list and start another, again we don’t want to needless work this out with every iteration, so it’s set just on the first item’s iteration.The break point will always be plus one because we’re not using a traditional counter which would usually increment at the end of an iteration(so the value is one higher that you may expect) and the call to
ceil
deals with cases when the menu has an odd amount of items, ie. if you had 9 items, it would start a new list after the 5th.Basically the only custom parts of this walker, are..
..and this..
..and finally this..
I’m pointing them out, so can pay attention to the important parts without wondering if anything else is relevant.
Lastly, you’ll also need to set you
wp_nav_menu
call to use a custom walker, which also means you’ll need to pass the args as an array, here’s an example.You can use this walker with any menu you like, just make sure when you use it, that you do so as i’ve shown above(setting the walker and menu args).
The big chunk of code(the walker) can go into your theme’s function file, if that’s easiest for you, it’s also where i tested it.
Hope that helps, and if you have any questions please let know.