2

I'm using the new wordpress custom post types and fields.

It's all working great, but for a custom image field (where I can select multiple images in one field), the value of this field when output is an array:

<?php
$field = get_post_meta($post->ID, "puma", false);
echo $field[0];
?>

This results in the following output (there are 3 images here):

180|177|174

These are clearly the image ID's stored in the wp_posts table of the database.

However, before I go mad trying to do this manually via an SQL query (hack), I was wondering if there is a better and more native way in wordpress to get the value of these or the proper way to output these images?

Cheers, Michael.

EDIT:

Thanks to some help I got below, the final code for anybody who needs it is this:

<?php

    $field = get_post_meta($post->ID, "myImageField", false);
    $str = $field[0] . "|"; // add an extra pipe at the end to get ALL the items (kinda tricking it.
    $theIDarray = explode('|', $str, -1);

    foreach ($theIDarray as $value) {

        echo wp_get_attachment_image($value, "myCustomImageSize");

    }

?>

This works for a custom field with multiple image selections for the 'content-types-wordpress-plugin'. Hope it helps those in need!

1 Answer 1

3

I think below function can return image, if you pass image id as parameter :

echo wp_get_attachment_image($image_id)

Sign up to request clarification or add additional context in comments.

1 Comment

Ok that worked great. In conjunction with the loop code I created, I can now output multiple images from one field. To anyone reading this, I will add my final loop code for the custom image field in my original question above. Hope this helps somebody.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.