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:

    mytheme/functions.php

    require_once('classes/FunctionClass.php');
    $fn = new MyDev\FunctionClass();
    add_filter('wp_get_nav_menu_items',array($fn,'buildSubbrandMenu'),10,2);

    mytheme/classes/FunctionClass.php

    namespace MyDev;
    class FunctionClass{
            private    $_walker;
    	public function buildSubbrandMenu($menu, $args){
    		 if(\is_admin() || $args->slug !== 'subbrand-menu'){
    			return $menu;
    		 }
    		if(empty($this->_walker)){
    			require_once('MyWalker.php');
    			$this->_walker = new MyWalker;
    		}
    		\wp_nav_menu(array(
    			'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
    		));
    	}
    }

    mytheme/classes/MyWalker.php

    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

    (@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.

    Thread Starter maxxd

    (@maxxd)

    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!

    Thread Starter maxxd

    (@maxxd)

    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:

    functions.php:

    require_once('classes/FunctionClass.php');
    $fn = new MyDev\FunctionsClass();
    add_filter('walker_nav_menu_start_el',array($fn,'addSubbrandDescription'),10,2);

    classes/FunctionsClass.php:

    namespace MyDev;
    class FunctionsClass{
        public function addSubbrandDescription($output, $item){
            if(empty($item->description)){
                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.