• Resolved clareswindlehurst

    (@clareswindlehurst)


    I hope someone can help me with this one as I have been going around in circles for hours!

    I have created a custom field that allows the user to browse for an image in the media gallery.

    I now want to output this image in the header but I can’t work out how to pass the information from get_post_meta (which pulls the attachment id) to wp_get_attachment_image (to convert that ID into an image).

    I’ve tried all kinds of combinations and none of them work – I want to next one inside the other but of course I can’t do that – and I’m not as well versed in PHP as I need to be to work it out.

    Here’s what I have tried most recently:

    <?php
    global $wp_query;
    $postid = $wp_query->post->ID;
    echo get_post_meta($postid, ‘hh-product-image’, true);
    ?>
    <?php echo wp_get_attachment_image($post->ID, ‘hh-product-image’, true); ?>

    I have successfully used the following to display the text in the product name field so I know the premise of calling the post information from outside the loop works:

    <?php
    global $wp_query;
    $postid = $wp_query->post->ID;
    echo get_post_meta($postid, ‘hh-product-name’, true);
    ?>
    <?php echo wp_get_attachment_image($post->ID, ‘hh-product-name’, true); ?>

    Any ideas?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter clareswindlehurst

    (@clareswindlehurst)

    Amazing what a night’s sleep can do.

    Here is the answer in case anyone else needs it:

    <?php
    global $wp_query;
    $postid = $wp_query->post->ID;
    $imageid = get_post_meta($postid, ‘hh-product-image’, true);
    ?>
    <?php echo wp_get_attachment_image($imageid, $size=array(70,70)); ?>

    You can even make it smaller:

    <?php $imageid = get_post_meta($post->ID, ‘hh-product-image’, true); ?>
    <?php echo wp_get_attachment_image($imageid, $size=array(70,70)); ?>

    You said that you “created a custom field that allows the user to browse for an image in the media gallery”. Can you explain how you did that? Is that a “textfield” as the type?

    It’s resolved 50% of my problem, but i need multiple images with plugin, in this case:

    [Images]
    type = file
    multiple = true
    multipleButton = true

    In my case, i resolved with this.

    My final sample with little change (convert ID key in image with wp_get_attachment_image):

    <?php foreach(get_post_custom_values(‘Images’) as $value) : ?>
    <?php echo wp_get_attachment_image($value, $size=array(70,70)); ?>
    <?php endforeach; ?>

    This in the loop of WP.

    Yes, i don’t know foreach in PHP and WP. Sorry. I’m initial student in this. I can see all my images now!!!

    Tks for all.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘[Plugin: Custom Field Template] Use Media Picker and output image’ is closed to new replies.