4

I'm using the code below to create custom menu items on the fly. Its working great, except all menus are created as "Custom". In the code below, setting the menu-item-type to 'page' appears to have no effect.

        foreach($thePages as $page){
            wp_update_nav_menu_item($menu->term_id, 0, array(
                'menu-item-title' => $page->post_title,
                'menu-item-type' => 'page', 
                'menu-item-status' => 'publish')
            );          
        }

wp-includes/nav-menu.php shows that the value is hard coded to "Custom". However, if I manually add a page to the menu, using the WP Menus manager, it lists it as "Page", not "Custom". What can I do to set the menu type to "Page"?

  • I've used the filter 'nav_menu_css_class' to modify the css classes. Is this what you wanted? – Cristian Apr 21 '11 at 19:36
  • @Wild Phoenix: I'm creating a custom menu on the fly, then iterating over the top level pages and assigning each of them to the custom menu. This is all done via a plugin during its activation hook. Its not related to css at all at this point. I'm just trying to figure out how to get each menu registered as a "Page" and not as "Custom" in my code above. – Scott B Apr 21 '11 at 19:42
  • try setting menu-item-object-id in your array to the page id – Bainternet Apr 21 '11 at 19:45
  • @baininternet: Will do. Thanks to the help and suggestion. – Scott B Apr 28 '11 at 17:18
  • @bainternet: This didn't work. Still shows as "Custom". 'menu-item-object-id' => $page->ID – Scott B Apr 29 '11 at 19:00
1

There's a filter:

function wpse15368_update_menu_item_type( $args )
{
    return $args['menu-item-type'] = 'page';
}
add_action( 'wp_update_nav_menu_item', 'wpse15368_update_menu_item_type' );
| improve this answer | |
  • 1
    As @lulalala below points out, post_type is indeed the key word to use. @lulalala has the right answer. – surfbuds Feb 15 '17 at 18:14
8

Strange it maybe, `post_type' is the keyword to use:

'menu-item-object' => 'page',
'menu-item-type' => 'post_type',
'menu-item-object-id' => $post_id

This will make a menu item just like you manually created one.

| improve this answer | |
  • 1
    This should be marked as the correct answer, thanks for sharing. – Studio4 Apr 27 '17 at 14:46

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.