I'm building a contact information plugin that writes data to a page. So far, so good. What I would like is to build a function into the plugin that adds a navigation item to the main nav bar on the site (there will only ever be one nav bar at a time), and ensures the item is at the bottom of the nav bar.
1 Answer
this is the general gist of what you need to do, you'll want to add some checks in here, make sure you get a valid menu returned, etc..
$locations = get_theme_mod( 'nav_menu_locations' );
$menu = wp_get_nav_menu_object( reset( $locations ) ); // get first item in menu locations array
$menu_item_data = array(
'menu-item-object-id' => $your_page->ID, // ID of the page you want to add
'menu-item-parent-id' => 0, // top level menu item
'menu-item-position' => 0, // setting position to 0 will add it to the end
'menu-item-object' => 'page',
'menu-item-type' => 'post_type',
'menu-item-status' => 'publish'
);
wp_update_nav_menu_item( $menu->term_id, 0, $menu_item_data );
-
Many thanks; I'll give it a shot and return with my success. Sorry if this is a basic thing :S Commented Sep 17, 2012 at 17:21
-
Was able to add menu items automatically thanks to this code. Thank you! Commented Sep 24, 2012 at 17:52
-
-
Warning: reset() expects parameter 1 to be array, boolean given Commented Oct 28, 2015 at 6:19