1

After each menu item, I want to add the page excerpt underneath it.

So if i have a menu that looks like this:

  • Home
  • About
  • Contact

I want to get the page excerpt into the menu like this:

  • Home
    • This is the home page excerpt
  • About
    • This is the about page excerpt
  • Contact
    • This is the contact page excerpt

Can I do this using the parameters in the wp_nav_menu() call? Or do I have to make a custom Nav Walker?

1

wp_nav_menu() calls walk_nav_menu_tree() which calls the Walker_Nav_Menu class.

There are a couple of filters you can hook into in those functions but I would take a look at the nav_menu_item_title filter in the Walker_Nav_Menu class. Here is the source:

/**
* Filter a menu item's title.
*
* @since 4.4.0
*
* @param string $title The menu item's title.
* @param object $item  The current menu item.
* @param array  $args  An array of {@see wp_nav_menu()} arguments.
* @param int    $depth Depth of menu item. Used for padding.
*/
$title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth );

So you could write a filter like so:

function my_nav_menu_item_title( $title, $item, $args, $depth ) {
    $title .= '<span class="excerpt">' . $item->post_excerpt . '</span>';
    return $title;
}
add_filter( 'nav_menu_item_title', 'my_nav_menu_item_title', 10, 4 );

I haven't tested this but it should lead you in the right direction. I would inspect what is contained in $item.

1

Solved thanks to kingkool68

function my_nav_menu_item_title( $title, $item, $args, $depth ) {
  $pid = $item->object_id;

  $text = get_the_excerpt($pid);

  $title .= '<span class="excerpt">' . $text . '</span>';
  return $title;

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.