0

If a new page is added under a parent page (front end), which is part of a nav menu, I want the new page to be added to the appropriate submenu automatically - without any plugin.

Reason: A custom user role has all caps to add, edit, delete posts and pages. However, this role shall not be capable to enter Design settings where Menu settings are part of. In my custom template, sub-pages simply should appear in the sub menu of their respective parents.

I understand, that I need to hook into the nav_menu render process. I guess, the first answer here is pretty close to a solution. However, I can't find the proper hook nor exactly how to hook in.

I fiddled around with wp_nav_menu_items but can't make sense of it, as the hook's callback function doesn't accept any parameters, does it?

That's my pseudo code to explain my intention:

function add_subpages_to_submenu($Menu){
    foreach ($Menu->Items as $Item) {
        if ($Item->hasChildren()){
            foreach ($Item->Children as $Child) {
                add_to_current_submenu($Child);
            }
        }
    }
    return $Menu;
}
add_filter('wp_nav_menu_items', 'add_subpages_to_submenu');

What needs this code to look like actually?

Thanks!

  • can you elaborate? this is back-end right? if the new page is added already under a parent page, you want to the new page to be added again? – David Lee Feb 22 '17 at 18:36
  • Added some more details to my question. It's all about front-end and custom template stuff. – Bunjip Feb 23 '17 at 10:27
2

Good evening.

I have to make this as an answer because of missing rep.

This post might help you out :)

Add child pages automatically to nav menu

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.