I have been trying to get the description for the parent menu item to show up to the right of the drop-down menu.
Like this:
But I cannot get the description to show up there at all. I have followed several tuts on this, including: This one that seems to have the same code as the other 5 or so I’ve seen.
I tried using the $item->description
but I’m not sure where that get’s populated in the walker class. I tried just reformatting the output, but to no avail.
Here is what I have:
class My_Walker_Nav_Menu extends Walker_Nav_Menu {
function start_lvl(&$output, $depth, $item) {
$indent = str_repeat("t", $depth);
$output .= "n$indent<section class="sub-menu col-12"><div class="nav-info col-8 right"><p>". $item->description ."</p></div>";
$output .= "n$indent<ul class="sub-menu-list col-4">n";
}
}
Everything works fine except there is no description where it says $item->description
. I know there is a description set in wordpress, but it’s not showing up.
What I need to know, I think, is where I pull the description from. The walker works, but the only think not showing up is the description.
Thanks in advance.
Using a combination of guesswork, and research, I’ve found the answer!
Using the tutorial I was looking at previously, I realized I needed to use separate methods in the class in order to add the description AND give the sub-menu
ul
a custom class.Here is the part that adds the description where it’s needed, before the sub-menu.
Afterwards, I used this method to give the sub-menu my custom class.
The problem was now that the formatting was off. An extra
div
kept showing up, and I realized I was never closing thesection
element, that would have normally been closed when used in thestart_lvl
method. Since it was in thestart_el
method it was not closing. So, I added this at the end to make sure both theul
andsection
would close properly.Now the description shows up as desired. Thank you to @Iliya Reyzis for your input. While it did not directly help me, I appreciate you attempting to help me.
When you are adding new walker, you need to bind it to your menu – wp_nav_menu() in the
walker
parameter like shown in the example –or you can filter all the menus
walker
parameter bydescription_walker
is the name of the walkeryou can try this walker and figure it out