Link to home
Create AccountLog in
Avatar of elRafa
elRafa

asked on

How to show wp_nav_menu item description - quick code fix

In WordPress 3's Appearance > Menus menu builder, there's an option to enter a description for the menu item, but no built-in way to show these descriptions on the front end. I found the code attached at Justin Tadlock's blog.

I've pasted it into the functions.php file of my child theme and it definitely does something. However, you can probably see by just looking at the last line of code above that the output is going to be messed up. Check the attached screenshot for one example of how it comes out. Sometimes (so far only with Japanese titles) nav items with only a title and no description are repeated immediately after a '<'. (Safari 5.0.3 on Mac OS X 10.6.5)

WHAT I NEED
I'd like for the description text to appear immediately below the linked title text but still within the LI tag.
See the second code snippet for desired output format.

If at all possible, I'd like to be able to use HTML in the description field too - however, that's just a bonus and not necessary - the first person to fix the code wins!
// Hook nav rules to showing description page

// Look details information by this page wp-includes/nav-menu-template.php

// or by this link - http://core.svn.wordpress.org/trunk/wp-includes/nav-menu-template.php

add_filter('walker_nav_menu_start_el', 'description_in_nav_el', 10, 4);

function description_in_nav_el($item_output, $item, $depth, $args){

    $attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';

    $attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';

    $attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';

    $attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';



    return preg_replace('/([^<]*?)</', "" . "{$item->title}{$item->post_content}<", $item_output);

}

Open in new window

<li id="(menu item ID here)" class="(menu classes here)"><a href="example.url">(Menu item title here)</a><br/><span>(Menu Item Description field content)</span></li>

Open in new window

Screen-shot-2010-12-31-at-5.28.4.png
Avatar of Sam Cohen
Sam Cohen
Flag of United States of America image

Are you try to show just a small subtitle or a paragraphed description in the nav?
You can install a plugin called: WP subtitle.
And this add another field right under your page title field. Once you add your subheading description... just enable it in the nav code.

<?thesubtitle()?> is used for inside The Loop -- if you wish to get a page/post's subtitle outside The Loop, use <?getthe_subtitle($id)?>

http://wordpress.org/extend/plugins/wp-subtitle/
So once you install the plug, you can add :<?thesubtitle()?> to your nave code
Avatar of elRafa
elRafa

ASKER

@all4artz: thanks for the effort - but unfortunately it's not what I asked for. I'd like to just add the code to my functions.php file and be done with it, So I just need to fix the code above... to enable output of the description field in the custom menus WordPress offers in WP 3+. The code above works - just not correctly. Any idea how to fix it?
You're having a problem because the person who posted the comment didn't include a complete solution... he just gave a clue.

Look here for a complete solution: http://www.kriesi.at/archives/improve-your-wordpress-navigation-menu-output/
ASKER CERTIFIED SOLUTION
Avatar of elRafa
elRafa

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.