Support » Themes and Templates » Adding Parent Title to Sub-Menu with Walker

  • I am trying to add the parent title as a link within the ul tag of any sub menu. After numerous attempts and google searches, I have only been able to display the title in the right place but with the wrong information. I know that $element->ID needs to be replaced but I’m not sure what with…

    The output is incomplete as it will also including a link tag when it works. This is the code I have so far:

    class Walker_With_Title_Menu extends Walker_Nav_Menu{
    		function start_lvl(&$output, $depth) {
    		    $indent = str_repeat("\t", $depth);
    
            $parent_menu = get_post( $element->ID );
            if (!empty( $parent_menu )) {
                $menu_title = $parent_menu->post_title;
                $menu_url = $parent_menu->post_url;
            }
    		    $output .= "\n$indent<ul class=\"sub-menu level-".$depth."\"><a href=".$menu_url."><h2>$menu_title</h2></a>\n";
    		}
    }

    This is what the website looks like and how the menu should be displayed:

    Website Screenshot

  • The topic ‘Adding Parent Title to Sub-Menu with Walker’ is closed to new replies.