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

Create Wordpress Menu Items Dynmaicly in Hierarchical structure WordPress

  • REFUNDED

Im hitting a brick wall here. In my theme I have a primary-menu slug ID = 31
What im doing now is looping through a list and trying to add items and sub items
so say the list is


[0]=array('Item1,item2,item3')
[1]=array('Item1,item2,item3')
[2]=array('Item1,item2,item3')


What im trying to achive is add to the primary-menu in that exact order
so

Menu Item 0
Item 1
Item 2
Item 3

Menu Item 1
Item 1
Item 2
Item 3

Its just not working at all... here is the code im calling for the PARENT Menu Item 1



$miid=wp_update_nav_menu_item($menu->term_taxonomy_id, 0, array(
'menu-item-title' => __($post_content['post_title']),
'menu-item-classes' => 'home',
'menu-item-url' => $permlink,
'menu-item-status' => 'publish',
menu-item-type' => 'post_type'));



Here is the code im calling to add the children im looping through




wp_update_nav_menu_item($menu->term_taxonomy_id, 0, array(
'menu-item-title' => __($post_content['post_title']),
'menu-item-classes' => 'home',
'menu-item-url' => $permlink,
'menu-item-status' => 'publish',
'menu-item-type' => 'post_type',
'menu-item-parent-id'=>$miid));



Its just not even adding the menu items at all... but i know the code works
because i did a test run with dummy data and it adds the menu items
but when im loopig through it.. its just not doing it.. and i have no idea why
am i doing something wrong here... all im doing is looping through an array
of parents and childen - adding the parent - getting the ID looping through
the children adding them to the menu with the PARENT menu ID

Please help with a solution, Im putting up 30 bucks for this!

Ricky

EDIT::


This literally cost me all day - but I hope it helps someone..

wp_update_nav_menu_item

Does not Always update

wpm_term_relationship

Table... hence the items are added correctly but do not show... This literally cost me about 8 hours today
Thanks for your help anyway this was a real tuff one.. but I dont think anybody had the right answer for
me...


Answers (3)

2013-11-06

Arnav Joy answers:

where did you got this code to add menu item ?

wp_update_nav_menu_item

structure is as follows

wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item_data = array() )

* @param int $menu_id The ID of the menu. Required. If "0", makes the menu item a draft orphan.
* @param int $menu_item_db_id The ID of the menu item. If "0", creates a new menu item.
* @param array $menu_item_data The menu item's data.
* @return int The menu item's database ID or WP_Error object on failure.

can you describe some more points about your task


rmatakajr comments:

Im basically injecting a menu hierarchy to a predefined menu id

2013-11-06

John Cotton answers:

It's the menu term id you need not the term_taxonomy_id.

Ideally, you'd get that from wp_create_nav_menu, but you can also get it from:


$menus = wp_get_nav_menus( array('orderby' => 'name') );

foreach( $menus as $menu ) {
if( $menu->slug == 'menu I want' ) {
$menu_id = $menu->term_id;
}
}


rmatakajr comments:

Ok so I want to add the term_id now the taxonomy_id.. ok will give it a shot


rmatakajr comments:

Also Ideally I would like to grab the active menu of any theme
not one I define.. is there a way do tell which is the active menu?

Anyway I will try with the term ID and post back here..

Thanks!


rmatakajr comments:

I made a function


function ag_get_menu_term($slug){
$menus = wp_get_nav_menus( array('orderby' => 'name') );
foreach( $menus as $menu ) {
if( $menu->slug == $slug ) {
return $menu->term_id;
}
}
}


$menu_term_id=ag_get_menu_term('primary-menu');

That did not work.. it returns empty... any idea why?

primary-menu does exist

This function


function ag_get_theme_menu( $theme_location ) {
if( ! $theme_location ) return false;

$theme_locations = get_nav_menu_locations();
if( ! isset( $theme_locations[$theme_location] ) ) return false;

$menu_obj = get_term( $theme_locations[$theme_location], 'nav_menu' );
if( ! $menu_obj ) $menu_obj = false;

return $menu_obj;
}

$menu=ag_get_theme_menu('primary-menu');


Returns the menu object , so maybee I can get the term_id from that object then..
cause the other one didnt work


rmatakajr comments:

ok now im really stumped here... I did it by the term_id which is ID 31
Here is the looped code

$short_index is just a ++ with count 0
so if == 0 we are doing the TOP level menu item



if($short_index==0){
$short_array[$short_index]['menu_id']=wp_update_nav_menu_item($menu_term_id, 0, array(
'menu-item-title' => __($post_content['post_title']),
'menu-item-classes' => 'home',
'menu-item-url' => $permlink,
'menu-item-status' => 'publish',
'menu-item-type' => 'post_type',
));

doLog('Doing '.$short_index.' Index.. Create Top Level Container Menu ID '. $short_array[$short_index]['menu_id']);

}else{
//do the chidren for container
$short_array[$short_index]['menu_id']=wp_update_nav_menu_item($menu_term_id, 0, array(
'menu-item-title' => __($post_content['post_title']),
'menu-item-classes' => 'home',
'menu-item-url' => $permlink,
'menu-item-status' => 'publish',
'menu-item-type' => 'post_type',
'menu-item-parent-id'=>$short_array[$short_index-1]['menu_id']));

doLog('Doing '.$short_index.' Index.. Create Sub Level Container Menu ID '. $short_array[$short_index]['menu_id'].' With Parent '.$short_array[$short_index-1]['menu_id']);
}


Here is the log

06-11-2013, 19:10 - Doing 0 Index.. Create Top Level Container Menu ID 168
06-11-2013, 19:10 - Doing 1 Index.. Create Sub Level Container Menu ID 170 With Parent 168

Yet in my theme

[[LINK href="http://screencast.com/t/pEVqjhEPStSS"]]http://screencast.com/t/pEVqjhEPStSS[[/LINK]]

No Menu Items at all

It like its adding them to the database but its just not displaying theme... this is driving me nutso


rmatakajr comments:

This literally cost me all day - but I hope it helps someone..

wp_update_nav_menu_item

Does not Always update

wpm_term_relationship

Table... hence the items are added correctly but do not show... This literally cost me about 8 hours today
Thanks for your help anyway this was a real tuff one.. but I dont think anybody had the right answer for
me...


John Cotton comments:

I'm not sure this is going to help you any further, but WP wraps a

wp_defer_term_counting( true );
and
wp_defer_term_counting( false );

around it's menu term updates. I don't know why as the code doesn't really explain the purpose. But I've found in the past that doing some for my own menu adds can make a difference.

You don't say whether you just found out it wasn't working, or actually found an answer...might be a clue for you?

2013-11-06

Sébastien | French WordpressDesigner answers:

You could eventually use a code like this :


add_filter( 'wp_nav_menu_items', 'your_custom_menu_item', 10, 2 );
function your_custom_menu_item ( $items, $args ) {

/* customize this array with your values
each value is the ID of a post
*/
$your_array = array(
'23' => array('12','9','1'),
'15' => array('1','12','9'),
);

//you can use conditions like that :
//if (is_single() && $args->theme_location == 'primary') {

if($your_array) {
foreach($your_array as $k=>$v) {
$items .= '<li class="menu-item"><a href="'.get_permalink($value).'">'.get_the_title($value).'</a>
<ul class="sub-menu">
';
foreach($v as $value) {
$items .= '<li class="menu-item"><a href="'.get_permalink($value).'">'.get_the_title($value).'</a></li>';

}


$items .= '</ul></li>';
}
}

//add this line if you use a condition
//}
return $items;
}


rmatakajr comments:

Issue is here. Im working with just frame work files. this is not inside a plugin
or a theme.. im actually calling these

include_once('wp-config.php');
include_once('wp-load.php');
include_once('./wp-includes/wp-db.php');
include_once('./wp-admin/includes/taxonomy.php');
include_once('./wp-includes/post.php');
include_once('./wp-includes/nav-menu-template.php');
include_once('./wp-includes/nav-menu.php');
include_once('./wp-includes/theme.php');
include_once('./wp-includes/link-template.php');
include_once('./wp-includes/category.php');
include_once('./wp-includes/rewrite.php');


Into a custom file outside of wordpress.. and controlling it.
So I dont think I can have hooks added.


Sébastien | French WordpressDesigner comments:

try
i think it's not a problem