0

I have been searching. And searching. I have found many, many ways to get the submenu, but they all seem to include the <li> and classes. I do not want that. I want to get an array page IDs of a particular menu's subnavigation pages.

wp_list_pages gets me close, but it seems to be grabbing those from the pages area in WP, not the navigation. I deleted "Concrete" from the nav because it was not in the correct order using wp_list_pages. But Concrete still showed.

And wp_list_pages generates an array of the complete code that can't seem to break apart to do what I want.

I just need the submenus page IDs in an array. It would be 9 IDs.

Below is the menu item "menu-item-127" that is my target. I want to get the page IDs of it's subnav. I do not want to get an array of the menu with the <li><a> etc.

<li id="menu-item-127" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-127"><a href="/services/">Services</a>
  <ul class="sub-menu">
<li id="menu-item-511" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-511"><a href="/services/construction/">Construction</a></li>
<li id="menu-item-514" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-514"><a href="/services/environmental/">Environmental</a></li>
<li id="menu-item-517" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-517"><a href="/services/safety-management/">Safety Management</a></li>
<li id="menu-item-515" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-515"><a href="/services/mechanical/">Mechanical</a></li>
<li id="menu-item-510" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-510"><a href="/services/civil-construction/">Civil Construction</a></li>
<li id="menu-item-520" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-520"><a href="/services/concrete/">Concrete</a></li>
<li id="menu-item-516" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-516"><a href="/services/medical/">Medical</a></li>
<li id="menu-item-512" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-512"><a href="/services/consulting/">Consulting</a></li>
<li id="menu-item-513" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-513"><a href="/services/energy-fueling/">Energy/Fueling</a></li>
  </ul>
</li>
  • 1
    Have you looked into wp_get_nav_menu_items() ? – David Sword Jan 6 '18 at 19:24
1

You can try the following function:

function wpse_290320_get_page_ids_from_menu( $menu_id, $submenu_id )
{
    $menus = wp_get_nav_menu_items( $menu_id );
    $pages = array();

    foreach( $menus as $menu )
    {
        if( $submenu_id == $menu->menu_item_parent && 'page' == $menu->object )
        {
            $pages[] = $menu->object_id;
        }
    }

    return $pages;
}

$menu_id = 26; 
$submenu_id = 3298;

$pages = wpse_290320_get_page_ids_from_menu( $menu_id, $submenu_id );

You can find the menu ID if you select a menu from Dashboard > Appearances > Menus, then in the URL. For example, if the URL is http://single.dev/wp-admin/nav-menus.php?action=edit&menu=26 then 26 is the menu ID.

And for submenu ID, according to the example, 127 is the submenu ID.

  • This is EXACTLY what I was looking for and works perfectly!! Thank you so much, I truly appreciate it. I was pulling my hair out. – Kirk Jan 7 '18 at 18:00
  • And if you want to get the IDs of ALL subnav items, pages AND posts, remove "&& 'page' == $menu->object" from the code leaving "if( $submenu_id == $menu->menu_item_parent)" – Kirk Jan 7 '18 at 18:21
  • Yes, I made it more specific as you wanted the "page" so I wanted to make sure to get only page IDs :) – Ashok Kumar Nath Jan 8 '18 at 9:41

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.