How can I access the “description” of a menu item?

I don’t like the idea to parse the menu items again. As an alternative solution I suggest to store the description during the first run:

add_filter( 'walker_nav_menu_start_el', 'wpse_78483_get_current_items_description', 10, 2 );

/**
 * Get nav items description.
 *
 * @wp-hook walker_nav_menu_start_el
 * @param   string $item_output
 * @param   object $item
 * @return  string
 */
function wpse_78483_get_current_items_description( $item_output = NULL, $item = NULL )
{
    static $desc="";

    // The function is NOT called during nav menu rendering, but later.
    if ( 'walker_nav_menu_start_el' !== current_filter() )
        return $desc;

    // The function is called during wp_nav_menu().

        // description is set
    if ( ! empty ( $item->description )
        // and an URL is available
        and ! empty ( $item->url )
        // and it is the current page
        and parse_url( $item->url, PHP_URL_PATH ) === $_SERVER['REQUEST_URI']
        )
    {
        // copy the description into our static internal variable
        $desc = $item->description;
        // remove this filter, it is not needed anymore
        remove_filter( 'walker_nav_menu_start_el', __FUNCTION__ );
    }

    // return unchanged item markup
    return $item_output;
}

Explanation

The function does two things:

  1. It acts as a filter called inside of wp_nav_menu(). Here, it is called until it hits the current page. Then the description is stored internally in $desc.
  2. It acts as a getter for the description: If you call this function without parameter after the navigation menu has been rendered you get the value of the description, if there is one.

The downside is: it would not work for a menu call too late, in a footer for example.
The advantage: you save time.

You can get the description later any time by calling the function without a parameter:

print wpse_78483_get_current_items_description();

As a follow-up, here is a second way to use it:

$desc = wpse_78483_get_current_items_description();

if ( empty ( $desc ) )
{
    the_excerpt();
}
else
{
    print wpautop( $desc );
}

Extra tip: You can enable the excerpt editor box for pages:

add_action( 'wp_loaded', 'wpse_78483_page_excerpt' );

function wpse_78483_page_excerpt()
{
    add_post_type_support( 'page', 'excerpt' );
}

Leave a Comment