Jump to content

WordPress menu modifications


maxxd

Recommended Posts

Hey all. I'm fairly new to WordPress (not php) 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'.

 

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.