Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

create a custom menu item WordPress

  • SOLVED

i have a custom menu
its ID=19

i duplicate this menu and i want to populate this new menu with the items of the first menu (id=19)

i use this code


$menu_id=19;
$menu_items = wp_get_nav_menu_items($menu_id);

$new_name "another menu";
$new_menu = wp_update_nav_menu_object( 0, array( 'menu-name' => $new_name ) );
$new_menu = wp_get_nav_menu_object($new_menu);


$new = array();

foreach($menu_items as $menu_item) {

$new = array(
'menu-item-db-id' => $menu_item->db_id,
'menu-item-object-id' => 0,
'menu-item-object' => '',
'menu-item-parent-id' => $menu_id,
'menu-item-position' => $menu_item->menu_order,
'menu-item-type' => $menu_item->type,
'menu-item-title' => $menu_item->post_title,
'menu-item-url' => $menu_item->url,
'menu-item-description' => $menu_item->post_content,
'menu-item-attr-title' => $menu_item->post_excerpt,
'menu-item-target' => '',
'menu-item-classes' => '',
'menu-item-xfn' => '',
'menu-item-status' => 0,
);

wp_update_nav_menu_item($new_menu->term_id, $menu_item_db_id = 0, $new);
}


the result is a menu with an invalide item
Look at the attachment.

probably there is a problem inthe mapping of array $menu to array $new
but i'm tired and i don't see the error.

Thx for your help :-)

Answers (5)

2011-12-17

Kannan C answers:

try this

$menu_id=19;
$menu_items = wp_get_nav_menu_items($menu_id);
$new_name = "another menu";
$new_menu = wp_update_nav_menu_object( 0, array( 'menu-name' => $new_name ) );
$new_menu = wp_get_nav_menu_object($new_menu);
$new = array();

foreach($menu_items as $menu_item) {

$new = array(
'menu-item-db-id' => 0,
'menu-item-object-id' => 0,
'menu-item-parent-id' => $menu_item->post_parent,
'menu-item-position' => $menu_item->menu_order,
'menu-item-title' => $menu_item->title,
'menu-item-url' => $menu_item->url,
'menu-item-description' => $menu_item->post_content,
'menu-item-attr-title' => $menu_item->post_excerpt,
'menu-item-target' => ''
);
wp_update_nav_menu_item($new_menu->term_id, $menu_item_db_id = 0, $new);

}


Sébastien | French WordpressDesigner comments:

Nice.

You have changed,
'menu-item-db-id' => 0,
and
'menu-item-parent-id' => $menu_item->post_parent,

Is that correct ?



Now, there is yet a valid item.

But there are two problems :
-this item is a custom (the original item is a category)
-the status of this item is not "publish"


Kannan C comments:

first thing i noticed is 'menu-item-title' => $menu_item->title
i am not sure about menu type, you can try using menu-item-type in the array (let me google for correct value for that. For publish simply include
'menu-item-status' => 'publish'


Sébastien | French WordpressDesigner comments:

'menu-item-status' => 'publish' seems to work :-))

But the is a problem :
-this item is a custom (the original item is a category)


Sébastien | French WordpressDesigner comments:

i have added
'menu-item-object' => $menu_item->object,
'menu-item-type' => $menu_item->type

and this problem seems resolved
but there is a big problem with the order
if i drag and drop utems, the ordre is not well saved


Kannan C comments:

this is working fine for me. verified the re-order.

$new = array(
'menu-item-db-id' => 0,
'menu-item-object-id' => $menu_item->object_id,
'menu-item-object' => $menu_item->type_label,
'menu-item-type' => $menu_item->type,
'menu-item-parent-id' => $menu_item->post_parent,
'menu-item-position' => $menu_item->menu_order,
'menu-item-title' => $menu_item->title,
'menu-item-url' => $menu_item->url,
'menu-item-description' => $menu_item->post_content,
'menu-item-attr-title' => $menu_item->post_excerpt,
'menu-item-status' => 'publish',
'menu-item-target' => ''
);


Sébastien | French WordpressDesigner comments:

Are you sur it's ok for you ?
have you test with several items (custom, category and page) ?

'menu-item-object' => $menu_item->type_label,
seems to be wrong
i replace by 'menu-item-object' => $menu_item->object,


Kannan C comments:

type_label and object is both working. Go with object.
yes its saving as where it is dropped. check screenshot