Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

So I thought I could do it like this (also I want to only get 3) -

$menu_items = wp_get_nav_menu_items(26);
$menu_items = array_rand($menu_items, 3);

foreach ( (array) $menu_items as $key => $menu_item ) {
    $title = $menu_item->title;
    echo $title;
}
share|improve this question
2  
So: Did you do it? How did it work out? What is the Q? –  kaiser Apr 26 '12 at 12:44

2 Answers 2

Ehm, afaik the return value of the menu fn is an object: You need to type cast earlier

$menu_items = (array) wp_get_nav_menu_items( 26 ); // Typecast here
$menu_items = array_rand( $menu_items, 3 );

foreach ( $menu_items as $key => $menu_item ) 
{
    echo $menu_item['title'];
}
share|improve this answer
    
No this didn't work. –  seanjacob Apr 27 '12 at 9:29
    
See update/edit. Slight misstake on my side. –  kaiser Apr 27 '12 at 11:22

Actually, the return of the menu function is an array of objects.

For some reason, the array_rand only returned an array of the IDs, so this is what I did:

$menu_items = wp_get_nav_menu_items('menu_slug');
$menu_items_rand = array_rand($menu_items, 3);
$random = array();
foreach($menu_items_rand as $ran)
    $random[] = $menu_items[$ran];

print_r($random)

You don't want to destroy the $menu_items array, because you'll need it in the foreach.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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