I want to obtain a path of all parents for current page. Something similar to breadcrub but used only for pages listed in menu.

For example if i have following structure:

L Menu
    L Sub menu
        L Current page
    L Sub menu 2
L Menu 2

I want to obtain:

Menu >> Sub Menu >> Current page

I am using WordPress 5.0.2. I tried various plugins but they show only current page instead of whole path.

How do i solve this issue?


The wp_get_nav_menu_items function will give you an array of menu item objects for a given menu. These objects contain a mixture of data specific to that unique menu item, as well as the post/page/link data the menu item refers to. You can use this array of menu items to find the current page, and then check the parent field of the item to get each parent in a loop.

Here's an example that should get you most of the way there. Use it by calling wpd_nav_menu_breadcrumbs( 'your menu' ) in your template. Read the comments to see what's happening in each section.

// helper function to find a menu item in an array of items
function wpd_get_menu_item( $field, $object_id, $items ){
    foreach( $items as $item ){
        if( $item->$field == $object_id ) return $item;
    return false;

function wpd_nav_menu_breadcrumbs( $menu ){
    // get menu items by menu id, slug, name, or object
    $items = wp_get_nav_menu_items( $menu );
    if( false === $items ){
        echo 'Menu not found';
    // get the menu item for the current page
    $item = wpd_get_menu_item( 'object_id', get_queried_object_id(), $items );
    if( false === $item ){
    // start an array of objects for the crumbs
    $menu_item_objects = array( $item );
    // loop over menu items to get the menu item parents
    while( 0 != $item->menu_item_parent ){
        $item = wpd_get_menu_item( 'ID', $item->menu_item_parent, $items );
        array_unshift( $menu_item_objects, $item );
    // output crumbs
    $crumbs = array(); 
    foreach( $menu_item_objects as $menu_item ){
        $link = '<a href="%s">%s</a>';
        $crumbs[] = sprintf( $link, $menu_item->url, $menu_item->title );
    echo join( ' > ', $crumbs );

*disclaimer: not rigorously tested, use at your own risk

| improve this answer | |
  • The code works. I am using Polylang plugin to obtain two languages for page. Moreover i have two menus per language: main menu and footer menu. The following helps to get curent menu breadcrumb: wpd_nav_menu_breadcrumbs(wp_get_nav_menu_name('primary')) – hal Jan 22 '19 at 16:33

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.