- This topic has 4 replies, 2 voices, and was last updated 7 years, 3 months ago by
ebarroso.
-
AuthorPosts
-
-
October 22, 2014 at 9:08 am #2609
ebarrosoMemberI 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!!
-
October 22, 2014 at 9:15 am #2611
JasonKeymasterI 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! 🙂
-
October 22, 2014 at 10:10 am #2612
-
-
October 22, 2014 at 10:21 am #2613
ebarrosoMemberSorry, in my first post of this Topic, i wrote “img_sec” and then “sec_img”, I have fixed, but I get the same error.
-
October 22, 2014 at 10:40 am #2614
ebarrosoMemberSolved:
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!!
-
-
AuthorPosts
- The topic ‘Add a post image with piklist’ is closed to new replies.