Support » Plugins » Hacks » Custom menu walker question

  • Hey all. I’m fairly new to WordPress (not php) and completely new to this forum, and am running into an issue. I need to add link descriptions to 1 of 5 menus on my page. I’ve read about extending Walker_Nav_Menu() and am attempting that, and it’s kinda going well except for one slightly annoying issue – it’s returning the wrong menu.

    I’m attempting to isolate the action to a menu with the slug ‘subbrand-menu’. All the code follows:


    $fn = new MyDev\FunctionClass();


    namespace MyDev;
    class FunctionClass{
            private    $_walker;
    	public function buildSubbrandMenu($menu, $args){
    		 if(\is_admin() || $args->slug !== 'subbrand-menu'){
    			return $menu;
    			$this->_walker = new MyWalker;
    			'menu'			=> 'subbrand_menu',
    			'container'		=> 'div',
    			'container_class'	=> 'menu-subbrand-menu-container',
    			'container_id'		=> '',
    			'menu_class'		=> 'menu',
    			'menu_id'		=> 'menu-subbrand-menu',
    			'echo'			=> true,
    			'walker'		=> $this->_walker


    namespace MyDev;
    class MyWalker extends \Walker_Nav_Menu{
    	public function end_el(&$output, $item, $depth=0, $args=array()) {
    		$output .= "<span class='thisisadescr'>Testing, testing</span></li>";

    Now, the above actually works quite well except that it’s outputting the contents of a menu with the slug ‘floater’. Of course, the hard-coded ‘descriptions’ are attached to each menu item, which is good in the long run…

    Anybody have any ideas what I’m missing here? I’m sure it’s something simple but it’s driving me crazy right now. Thanks in advance for any and all thoughts, hints, tips, and ideas.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz


    Everything appears to be in order. The only explanation is the not match menu slug logic is not working for some reason. Sometimes using != instead of !== will make a difference even though it should not matter. Try dumping $args->slug to ensure the slug is what you think it should be.

    There’s some strange backslashes creeping into your posted code here and there, there could be a charset discrepancy. PHP code pages should be utf-8 with no BOM. Also try to ensure there are no hanging spaces at the end of your lines. Most programmer editors have a macro to clean these out.

    That’s all I can think of. You probably know all of this already, but for me, sometimes I need a reminder of something obvious I’d overlooked. That and for the benefit of anyone else landing here, I mention these anyway.

    I appreciate you’re mentioning them!

    $args->slug returns ‘subbrand-menu’, which is expected, and when I print_r($menu), the menu option objects are the subbrand-menu menu. It’s just printing the floater menu instead.

    The backslashes are due to my using namespaces. I’ll check again on my encoding – I know that I set it to utf-8, but weird things happen sometimes. And I’ll try with the general not-equal-to instead of the exact match that I’m using (pretty sure I’ve tried that as well, but this has been kicking me in the head for a couple days and I may not have…)

    Thank you very much!

    In case anyone’s experiencing a similar issue, I didn’t figure out what I did wrong with my code, but I did find a hack around the entire thing here. I adapted it somewhat as such:


    $fn = new MyDev\FunctionsClass();


    namespace MyDev;
    class FunctionsClass{
        public function addSubbrandDescription($output, $item){
                return $output;
            return "{$output}<span class='thisisadescription'>".__($item->description)."</span>";

    Just to update the topic – I’m hoping at some point to figure out what it was that I was doing wrong in the custom walker code. Thanks again bcworkz for taking a look and offering suggestions!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Custom menu walker question’ is closed to new replies.