8 posts
  • Elite Author
  • Weekly Top Seller
  • Trendsetter
  • Author Level 7
+4 more
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;
70 posts Talk less do more!
  • Weekly Top Seller
  • Affiliate Level 2
  • Author Level 7
  • Collector Level 3
+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
  • Elite Author
  • Weekly Top Seller
  • Trendsetter
  • Author Level 7
+4 more
ThemeRain
says

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

70 posts Talk less do more!
  • Weekly Top Seller
  • Affiliate Level 2
  • Author Level 7
  • Collector Level 3
+3 more
Rifki
says

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

8 posts
  • Elite Author
  • Weekly Top Seller
  • Trendsetter
  • Author Level 7
+4 more
ThemeRain
says

I checked, array not empty.

8 posts
  • Elite Author
  • Weekly Top Seller
  • Trendsetter
  • Author Level 7
+4 more
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 ) )
70 posts Talk less do more!
  • Weekly Top Seller
  • Affiliate Level 2
  • Author Level 7
  • Collector Level 3
+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
  • Elite Author
  • Weekly Top Seller
  • Trendsetter
  • Author Level 7
+4 more
ThemeRain
says

It’s not help :(

8 posts
  • Elite Author
  • Weekly Top Seller
  • Trendsetter
  • Author Level 7
+4 more
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>

70 posts Talk less do more!
  • Weekly Top Seller
  • Affiliate Level 2
  • Author Level 7
  • Collector Level 3
+3 more
Rifki
says
You can do with:
$thumbnail = '<div class="rain">'.$gallery.'</div>';
by
by
by
by
by
by