8 posts
  • Has sold $40,000+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Sells items exclusively on Envato Market
  • Has been part of the Envato Community for over 2 years
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!
  • Has referred 10+ members
  • Has sold $75,000+ on Envato Market and is now an Elite Author
  • Has collected 50+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+3 more
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
  • Has sold $40,000+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Sells items exclusively on Envato Market
  • Has been part of the Envato Community for over 2 years
ThemeRain says

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

60 posts Talk less do more!
  • Has referred 10+ members
  • Has sold $75,000+ on Envato Market and is now an Elite Author
  • Has collected 50+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+3 more
Rifki says

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

8 posts
  • Has sold $40,000+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Sells items exclusively on Envato Market
  • Has been part of the Envato Community for over 2 years
ThemeRain says

I checked, array not empty.

8 posts
  • Has sold $40,000+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Sells items exclusively on Envato Market
  • Has been part of the Envato Community for over 2 years
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!
  • Has referred 10+ members
  • Has sold $75,000+ on Envato Market and is now an Elite Author
  • Has collected 50+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+3 more
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
  • Has sold $40,000+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Sells items exclusively on Envato Market
  • Has been part of the Envato Community for over 2 years
ThemeRain says

It’s not help :(

8 posts
  • Has sold $40,000+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Sells items exclusively on Envato Market
  • Has been part of the Envato Community for over 2 years
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!
  • Has referred 10+ members
  • Has sold $75,000+ on Envato Market and is now an Elite Author
  • Has collected 50+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+3 more
Rifki says
You can do with:
$thumbnail = '<div class="rain">'.$gallery.'</div>';
by
by
by
by
by
by