8 posts
  • Exclusive Author
  • Bought between 1 and 9 items
  • Has been a member for 1-2 years
  • Sold between 10 000 and 50 000 dollars
ThemeRain says

Hi, I try to get all titles value from metabox, but i get “Invalid argument supplied for foreach()” and only last title from array.

$wp_query = new WP_Query(array('post_type' => 'post', 'posts_per_page' => '3')); while($wp_query->have_posts()) : $wp_query->the_post(); $slides = get_post_meta($wp_query->post->ID, 'meta_box_gallery', true); foreach($slides as $slide) { $gallery = $slide['title']; } $gallery endwhile;
60 posts Talk less do more!
  • Elite Author
  • Exclusive Author
  • Has been a member for 2-3 years
  • Sold between 50 000 and 100 000 dollars
  • Bought between 50 and 99 items
  • Referred between 10 and 49 users
  • Indonesia
Rifki says

Maybe the problem is get_post_meta($wp_query->post->ID, 'meta_box_gallery', true); try with this: get_post_meta($post->ID, 'meta_box_gallery', true);. Good luck!

8 posts
  • Exclusive Author
  • Bought between 1 and 9 items
  • Has been a member for 1-2 years
  • Sold between 10 000 and 50 000 dollars
ThemeRain says

Unfortunately no, with WP_Query I need to use $wp_query->post->ID

60 posts Talk less do more!
  • Elite Author
  • Exclusive Author
  • Has been a member for 2-3 years
  • Sold between 50 000 and 100 000 dollars
  • Bought between 50 and 99 items
  • Referred between 10 and 49 users
  • Indonesia
Rifki says

Check the array values, make sure that is not an empty array. print_r( $slides );

8 posts
  • Exclusive Author
  • Bought between 1 and 9 items
  • Has been a member for 1-2 years
  • Sold between 10 000 and 50 000 dollars
ThemeRain says

I checked, array not empty.

8 posts
  • Exclusive Author
  • Bought between 1 and 9 items
  • Has been a member for 1-2 years
  • Sold between 10 000 and 50 000 dollars
ThemeRain says

print_r($slides) show me:

Array ( [0] => Array ( [title] => 1 [upload_gallery_image] => http://website.com/image1.jpg ) [1] => Array ( [title] => 2 [upload_gallery_image] => http://website.com/image2.jpg ) )
60 posts Talk less do more!
  • Elite Author
  • Exclusive Author
  • Has been a member for 2-3 years
  • Sold between 50 000 and 100 000 dollars
  • Bought between 50 and 99 items
  • Referred between 10 and 49 users
  • Indonesia
Rifki says

Hmm.. I think this is what you’re looking :

$wp_query = new WP_Query(array('post_type' => 'post', 'posts_per_page' => '3')); 

while($wp_query->have_posts()) : $wp_query->the_post();
    $slides = get_post_meta($wp_query->post->ID, 'meta_box_gallery', true); 

    foreach($slides as $slide) { 
        $gallery = $slide['title']; 

        // let show
        echo "Title: $gallery \n";
    } 
endwhile;

Edit: The code above works for me

8 posts
  • Exclusive Author
  • Bought between 1 and 9 items
  • Has been a member for 1-2 years
  • Sold between 10 000 and 50 000 dollars
ThemeRain says

It’s not help :(

8 posts
  • Exclusive Author
  • Bought between 1 and 9 items
  • Has been a member for 1-2 years
  • Sold between 10 000 and 50 000 dollars
ThemeRain says

Yeah, edited code is works, that’s great! But how I can put this values instead $gallery not using echo?

$thumbnail = '<div class="rain">$gallery</div>

60 posts Talk less do more!
  • Elite Author
  • Exclusive Author
  • Has been a member for 2-3 years
  • Sold between 50 000 and 100 000 dollars
  • Bought between 50 and 99 items
  • Referred between 10 and 49 users
  • Indonesia
Rifki says
You can do with:
$thumbnail = '<div class="rain">'.$gallery.'</div>';
by
by
by
by
by
by