1

I know that's possible to limit the depth that navigation menu items can be given children like this:

wp_add_inline_script('nav-menu', 'wpNavMenu.options.globalMaxDepth = 1;', 'after');

My issue is that I have three menus, and I only want to limit the depth on two of them. Is it possible to conditionally add the above when particular menus are being administered?

Limiting the depth displayed when actually rendering the menus in my theme is a last resort. I'd rather have the menu created in admin match the menu actually shown.

1 Answer 1

1

to know which menu is selected, you can do that in JavaScript

    var selected_menu_id = $("#select-menu-to-edit option:selected").prop("value");

    if ("56" === selected_menu_id) {
        wpNavMenu.options.globalMaxDepth = 2;
    }
3
  • Looks like a good approach, however I'd prefer to to have to rely on the menu's ID as that is bound to differ between environments. Commented Oct 30, 2017 at 8:27
  • How do you define which menu must have this depth restriction if it's not by ID ?
    – mmm
    Commented Oct 30, 2017 at 8:30
  • Using wp_get_nav_menu_object with the ID retrieved is working well. Thank you. Commented Oct 30, 2017 at 8:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.