Hi there,
I am using Advanced Custom Fields to add images to custom posts. I then want to grab the image with field name ‘product_image’ to use with Get The Image:
<?php get_the_image( array( 'meta_key' => array( 'product_image', 'Thumbnail', 'thumbnail' ), 'size' => 'thumbnail', 'default_image' => get_stylesheet_directory_uri() . '/images/placeholder.jpg' ) ); ?>
This almost works in that it successfully grabs the image ID, returning <img src="425"...
.
Any tips for getting the full path?
Thanks!
@CarolineElisa
To get the URL from an attachment ID you can use:
<?php wp_get_attachment_image_src( $attachment_id, $size, $icon ); ?>
where $size (str|array) and $icon (bool) are optional.
Thanks! I’ve ended up using this in order to still have a fallback image using Get The Image:
<?php $attachment_id = get_field('product_image');
$size = "thumbnail";
$image = wp_get_attachment_image_src( $attachment_id, $size );
if ( $image != 0 ) { ?>
<img src="<?php echo $image[0]; ?>" />
<?php } else {
get_the_image( array( 'size' => 'thumbnail', 'default_image' => get_stylesheet_directory_uri() . '/images/placeholder.jpg' ) );
} ?>