Skip to content

Instantly share code, notes, and snippets.

@billerickson
Last active June 29, 2022 13:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save billerickson/1cc2c10068f203e6f1d4 to your computer and use it in GitHub Desktop.
Save billerickson/1cc2c10068f203e6f1d4 to your computer and use it in GitHub Desktop.
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
Copy link

@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
Copy link

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

@kostikovmu
Copy link

@oneblackcrayon, yes
$item_output .= "<span>$item->description</span>";
and add styles to span.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment