Tagged: ,

This topic contains 4 replies, has 2 voices, and was last updated by  ebarroso 4 years, 9 months ago.

  • Author
    Posts
  • #2609

    ebarroso
    Participant

    I want to add 2 image on a custom post. One of them, i have added with the WordPress feature, the other, i´m trying to add with Piklist but is not possible.
    I have been reading in this forum and trying the solution, but i cant fix it.

    This is my code in the custom-metabox.php:

    piklist('field', array(
      'type' => 'file'
      ,'field' => 'sec_img'
      ,'label' => 'Imagen secundaria'
    ));

    And this, in my single.php:

     <?php
    
                                          $image_ids = get_post_meta($post_id, 'img_sec');
    
                                          foreach ($image_ids as $image)
                                          {
                                            $myupload = get_post_meta($image);
    
                                            echo '<img src="' . wp_get_attachment_url($image) . '"/>';
    
                                            print_r($myupload); // Displays all data
                                          }
                                        ?>

    And I obtain this error in frontend:

    “Warning: Invalid argument supplied for foreach() in D:\xampp\htdocs\wordpress\wp-content\themes\MyBlog\single.php on line 55”

    And, no image is shown.

    Thanks!!

  • #2611

    Jason
    Keymaster

    I believe the file field stores it as a serialized single row. Try:
    $image_ids = get_post_meta($post_id, 'img_sec', true);

    Since you’re already using print_r, I suggest using that on $image_ids to help you know the structure of what you’re dealing with.

    Hope this helps! 🙂

    • #2612

      ebarroso
      Participant

      Hi @jason, thanks for your help, but i get the same error, so, nothing is printed with my print_r($image_ids).
      I dont know what happen…

  • #2613

    ebarroso
    Participant

    Sorry, in my first post of this Topic, i wrote “img_sec” and then “sec_img”, I have fixed, but I get the same error.

  • #2614

    ebarroso
    Participant

    Solved:

    It was wrong: $image_ids = get_post_meta($post_id, 'sec_img', true);

    The correct form is: $image_ids = get_post_meta($post->ID, 'sec_img', true);

    So, my final code, to show the second image with Piklist, is:

    <?php
    
    $image_ids = get_post_meta($post->ID, 'sec_img');
    foreach ($image_ids as $image)
    {
    $myupload = get_post_meta($image);
    echo '<img src="' . wp_get_attachment_url($image) . '"/>';
    }
    ?>

    Thanks for your help!!

The topic ‘Add a post image with piklist’ is closed to new replies.