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>
173

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.

People are also looking for solutions to the problem: php - drop down value not being submitted to post array

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.