php - WordPress parent-child menu HTML
94
Suppose I have this simple menu with a parent-child item:
- Home
- About us
- Parent page
- childpage 1
- childpage 2
WordPress just stacks these links next to eachother as if they are all equal in hierarchial order (which they aren't).
<ul>
<li><a href="#">home</a></li>
<li><a href="#">home</a></li>
<li><a href="#">Parent</a></li>
<li><a href="#">childpage 1</a></li>
<li><a href="#">childpage 2</a></li>
</ul>
I usewp_nav_menu();
to retrieve this menu. Is there any way so that it intelligently outputs this menu as:
<ul>
<li><a href="#">home</a></li>
<li><a href="#">home</a></li>
<li><a href="#">Parent</a>
<ul>
<li><a href="#">childpage 1</a></li>
<li><a href="#">childpage 2</a></li>
</ul>
</li>
</ul>
Answer
Solution:
The WordPress dynamic menus are independent of the WordPress parent/child relationships for pages. Menus themselves are an independent post type.
If you want to have your menu as output with submenus you have to create the menu in Design -> Menus and drag & drop the items to the right side to be child items.
With the custom menu functionality of WordPress you could even build menus where child pages are parents in the menu and parent pages are childs in the menu for instance.
The pages parent/child relationships are for better organization in the backend, for the permalinks and may matter in functions where you just list your pages.