How to get current page menu item name instead of full menu item list

  1. Within each menu item, object_id holds the ID of whatever object
    the menu item refers to. This will be a post / page / CPT / term ID. If it’s a custom link, the ID refers to itself. (If you want to know what type of object it is, object contains that.)

  2. get_queried_object_id() will give you the ID of the current page.

  3. We can use the API function
    wp_filter_object_list()
    to filter out any menu items where the object_id doesn’t match the
    queried object.

  4. We’ll then be left with an array containing a single element, so we
    use php’s current so that $this_item now contains just the
    matching menu item object.

  5. and finally, title holds the name we’ve given the menu item.

$menu_items = wp_get_nav_menu_items( 'main-menu' );
$this_item = current( wp_filter_object_list( $menu_items, array( 'object_id' => get_queried_object_id() ) ) );
echo $this_item->title;

Note- I didn’t test this, so hopefully it works!

Leave a Comment