0

I have a post meta for image uploads which allows a maximum of 5 files.

The data is stored in like this:

a:5:{i:0;s:3:"694";i:1;s:3:"694";i:2;s:3:"697";i:3;s:3:"695";i:4;s:3:"696";}

The problem is, I can't get the image URLs. My code:

                <?php $ppics = get_post_meta( get_the_ID(), 'shop_demosc', false );
            $ppurl = wp_get_attachment_url($ppics);
            foreach ($ppics as $key => $ppurl){
                echo '<img src="'. print_r($ppurl) .'">';
            } ?>

This code returns:

Array
 (
   [0] => 694
   [1] => 694
   [2] => 697
   [3] => 695
   [4] => 696
 )

whats wrong? How can I get the URLs to the 5 images?

4
  • Your code reads as: "fetch the URL of the post named { post1 and post2 and post3 and post4}", even if it did what you expected, the images would all have the same src value. Write out your code in pseudocode and you'll see that the plural and singular bits are all muddled up
    – Tom J Nowell
    Nov 7 '16 at 22:13
  • var_dump($ppics);
    – jgraup
    Nov 8 '16 at 5:08
  • @jgraup result is: array(1) { [0]=&gt; array(5) { [0]=&gt; string(3) "694" [1]=&gt; string(3) "694" [2]=&gt; string(3) "697" [3]=&gt; string(3) "695" [4]=&gt; string(3) "696" } }
    – arshidok
    Nov 8 '16 at 12:20
  • And var_dump($ppics[0]);?
    – jgraup
    Nov 8 '16 at 12:32
0

Your var_dump looks like it's returning an array because you're passing false to get_post_meta(). Also you don't want to print_r() on an echo line.

Retrieve post meta field for a post.

  • $post_id (int) (Required) Post ID.
  • $key (string) (Optional) The meta key to retrieve. By default, returns data for all keys. Default value: ''
  • $single (bool) (Optional) Whether to return a single value. Default value: false
<?php 

$ppics = get_post_meta( get_the_ID(), 'shop_demosc', true );

// if the value is an array of an array, just set to the first array
if( is_array($ppics) && count($ppics) === 1 && is_array($ppics[0]) ){
    $ppics = $ppics[0];
}

foreach ( $ppics as $key => $attachment_id ) {
    $image_url = wp_get_attachment_url( $attachment_id );
    printf( '<img src="%s">', $image_url );
} 

?>   
0
1

You need to move

    $ppurl = wp_get_attachment_url($value); // $value of the foreach

In the foreach loop

so your code might look like this

  <?php $ppics = get_post_meta( get_the_ID(), 'shop_demosc', true);// true to get the unserialize array directly

        foreach ($ppics as $key => $attachment_id){
            $ppurl = wp_get_attachment_url(attachment_id);
            echo '<img src="'. $ppurl .'">';
        } ?>
5
  • thanks, but not works. just return "1".
    – arshidok
    Nov 7 '16 at 22:51
  • Unserialize($ppics); before the loop if you leave the $single to false
    – Benoti
    Nov 7 '16 at 22:53
  • thank you so much again, but this time, return <img src="1"> 5times!
    – arshidok
    Nov 7 '16 at 22:59
  • What's your third parameter in get_post_meta ? I've edit my answer
    – Benoti
    Nov 7 '16 at 23:01
  • with your new code, now its just return nothing! im confused! why!
    – arshidok
    Nov 7 '16 at 23:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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