Skip to content

Instantly share code, notes, and snippets.

@billerickson

billerickson/functions.php Secret

Last active Mar 6, 2016
Embed
What would you like to do?
Descriptions on Header Menu
<?php
/**
* Descriptions on Header Menu
* @author Bill Erickson
* @link http://www.billerickson.net/code/add-description-to-wordpress-menu-items/
*
* @param string $item_output, HTML outputp for the menu item
* @param object $item, menu item object
* @param int $depth, depth in menu structure
* @param object $args, arguments passed to wp_nav_menu()
* @return string $item_output
*/
function be_header_menu_desc( $item_output, $item, $depth, $args ) {
if( 'header' == $args->theme_location && ! $depth && $item->description )
$item_output = str_replace( '</a>', '<span class="description">' . $item->description . '</span></a>', $item_output );
return $item_output;
}
add_filter( 'walker_nav_menu_start_el', 'be_header_menu_desc', 10, 4 );
@tyrann0us

This comment has been minimized.

Copy link

@tyrann0us tyrann0us commented Nov 25, 2015

@param string $item_output, HTML outputp for the menu item clerical mistake outputp.

I adjusted the function a little bit:

function be_header_menu_desc( $item_output, $item, $depth, $args ) {
    if( ! 'header' == $args->theme_location && $depth && ! $item->description ) return $item_output;
    $item_output = str_replace( '</a>', sprintf( '<span class="description">%s</span></a>', $item->description ), $item_output );
    return $item_output;
}
add_filter( 'walker_nav_menu_start_el', 'be_header_menu_desc', 10, 4 );
@oneblackcrayon

This comment has been minimized.

Copy link

@oneblackcrayon oneblackcrayon commented Mar 6, 2016

Hello,
Is there a way to make this work with putting the description in front of the <a href>?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.