I am building a website for a public school district. All of our school buildings have common pages with the same name (i.e. Library, Nurse, Attendance, etc) that are nested under their parent building's page. While trying to update our site navigation, I've noticed that for some reason the Menus -> Pages -> View All area is not nesting the building pages under their building and is instead listing them alphabetically.
The odd thing is, I have nested pages set up in other areas of the site and they do nest themselves on the list.
Am I missing something? I can't find anything different about the way I nested the pages that do display properly vs those that don't. I double checked the nested pages and they have their building listed as their parent.
I find this extremely frustrating. Unless I go in and edit all of the pages under a specific building to force them into the recent list I have no idea which page is which.
If there isn't a way to fix the way the list displays, is there maybe a way to add the parent page's name to the page name? Or another way to build the menus?
I am building a website for a public school district. All of our school buildings have common pages with the same name (i.e. Library, Nurse, Attendance, etc) that are nested under their parent building's page. While trying to update our site navigation, I've noticed that for some reason the Menus -> Pages -> View All area is not nesting the building pages under their building and is instead listing them alphabetically.
The odd thing is, I have nested pages set up in other areas of the site and they do nest themselves on the list.
Am I missing something? I can't find anything different about the way I nested the pages that do display properly vs those that don't. I double checked the nested pages and they have their building listed as their parent.
I find this extremely frustrating. Unless I go in and edit all of the pages under a specific building to force them into the recent list I have no idea which page is which.
If there isn't a way to fix the way the list displays, is there maybe a way to add the parent page's name to the page name? Or another way to build the menus?
For those who run into this in the future, the issue was due to pagination and the parent appearing on a different page than the children. Removing pagination from the View All menu fixed the nesting.
There is a ticket opened 7 years ago reporting this bug and it has yet to be resolved. In the comments user danburzo suggests adding the following filter, which corrected the issue for me.
<?php
add_filter( 'nav_menu_meta_box_object', array( $this, 'disable_pagination_in_menu_meta_box' ) );
function disable_pagination_in_menu_meta_box($obj) {
$obj->_default_query = array(
'posts_per_page' => -1
);
return $obj;
}
?>
Yes this is annoying, I found that this happens only if the [Main Navigation] option is checked ,go to menus on theme customization; uncheck this and you will be fine.
The function you're looking for is wp_list_pages()
.
Pay particular attention to these two arguments documented on that page.
depth
being the most relevant to your question.
'depth' (int) Number of levels in the hierarchy of pages to include in the generated list. Accepts -1 (any depth), 0 (all pages), 1 (top-level pages only), and n (pages to the given n depth). Default 0.
There's also child_of
, giving you the option to list only those that are children of a specific parent page that you have; i.e., a specific building's child pages.
'child_of' (int) Display only the sub-pages of a single page by ID. Default 0 (all pages).
Tip: One more option is to use a combination of get_pages()
and wp_list_pages()
as shown by example here. Between the two, you should be able to display the hierarchy as desired.