Community Forums › Forums › Archived Forums › Design Tips and Tricks › Menu description in navigation
Tagged: Menu description, walker_nav_menu_start_el
- This topic has 8 replies, 2 voices, and was last updated 8 years, 2 months ago by Brad Dalton.
-
AuthorPosts
-
December 10, 2015 at 6:32 am #173422unkleeMember
I am modifying the Minimum Pro theme. I have only very basic competency in PHP.
I want to add the menu descriptions to the secondary navigation menu, which is located in a widget area in the header. I found this discussion on this forum (related to a different theme), and it agreed with what I also found by Googling.
I added the code to the functions.php file, and the descriptions appear in the menu, and I can style them and make one appear below the other, but the descriptions appear before the menu titles, instead of after. I can't find the way to reverse them.
The piece of code I presume I have to modify is this:
return preg_replace('/(<a.*?>[^<]*?)</', '$1' . "<span class=\"menu-description\">{$item->attr_title}</span><br /><", $item_output);
but I'm afraid I don't really understand Preg-replace and I certainly don't understand all the parameters in this code.
Is there a simple answer to make the name appear before the description please?
December 10, 2015 at 10:16 am #173453Brad DaltonParticipantDecember 13, 2015 at 6:03 am #173656unkleeMemberThanks Brad, I'm sorry to delay replying, but I thought I had posted a reply but I must have done something wrong.
That reference was one of the ones I obtained via Google, and unfortunately, I couldn't get it to work at all - there was still only the menu item name, and not the description. The one I found on this forum was one of only two I found that displayed the description, but both displayed it first.
For the record, this was the entire code:
add_filter('walker_nav_menu_start_el', 'description_in_nav_el', 10, 4); function description_in_nav_el($item_output, $item, $depth, $args) { return preg_replace('/(<a.*?>[^<]*?)</', '$1' . "<span class=\"menu-description\">{$item->attr_title}</span><br /><", $item_output); }
Do you, or anyone else, have any ideas how to make the name appear before the description please?
December 13, 2015 at 6:56 am #173663Brad DaltonParticipantThis adds it after the menu item name based on my testing http://www.billerickson.net/code/add-description-to-wordpress-menu-items/
December 13, 2015 at 7:13 pm #173721unkleeMemberHi Brad, thanks again.
I tried the code your have referenced again, and it still did nothing. So I've been thinking about why it works for you but not for me.
My PHP is rudimentary, but it is clear what this code does - it searches for menu links and then adds extra code before the final "/a" tag to display the description as well as the name of the link. I think the "str_replace" function must be correct as it works for you, so I'm thinking the problem must be in the "if" test, which is:
if( 'header' == $args->theme_location && ! $depth && $item->description )
Now this is where I start to struggle. I'm guessing what this does is look for any header array which has as its arguments theme_location, $depth and $item->description, and this identifies the menu we want to display. My understanding is that:
theme_location is set for each menu location, and if not set a default menu is shown.
$depth is the number of levels in the menu hierarchy.
$item-> means the item we are looking at is the menu description.That all seems straightforward, except those parameters appear in a wp_nav_menu function, and I don't understand what 'header' actually is, where I find it, and how any of those arguments could be "wrong" for me but "right" for you? I also don't understand the "!" in the code.
Sorry to bug you, but have you any ideas please?
December 13, 2015 at 7:18 pm #173722Brad DaltonParticipantI spent several hours testing different solutuons and this is what i came up with
December 16, 2015 at 4:19 am #173897unkleeMemberOK, thanks for your help.
February 1, 2016 at 3:35 am #178023unkleeMemberJust to close this off, I got it working fine thanks. It was easy, I just had a silly mistake. Thanks again.
February 1, 2016 at 3:40 am #178025Brad DaltonParticipant -
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.