Home › Forums › General Issues › Function to Return Image Caption from ID not Image Object?
Anyone have any ideas on the best way to do this?
I absolutely can NOT use the image object type because we have a function that we use for all ACF images that builds the full srcset from them… it looks like this:
/**
* Responsive Image Helper Function
*
* @param string $image_id the id of the image (from ACF or similar)
* @param string $image_size the size of the thumbnail image or custom image size
* @param string $max_width the max width this image will be shown to build the sizes attribute
*/
function sitename_responsive_image($image_id,$image_size,$max_width){
// check the image ID is not blank
if($image_id != '') {
// set the default src image size
$image_src = wp_get_attachment_image_url( $image_id, $image_size );
// set the srcset with various image sizes
$image_srcset = wp_get_attachment_image_srcset( $image_id, $image_size );
// generate the markup for the responsive image
echo 'src="'.$image_src.'" srcset="'.$image_srcset.'" sizes="(max-width: '.$max_width.') 100vw, '.$max_width.'"';
}
}
Then usage looks like this:
<?php if( get_sub_field('image') ): ?>
<img class="panel-image mb-3" <?php sitename_responsive_image(get_sub_field( 'image' ),'full','768px'); ?> alt="text" />
<?php endif; ?>
So I either need to figure out how to adapt this function to pull from the object instead of the image ID, OR need to figure out a new function that I can use to just return the image caption.
I would get the caption another way, this has a good example https://stackoverflow.com/questions/34658739/wordpress-get-attachment-image-caption
Im getting nowhere on this. So frustrating
Is caption even part of the array from wp_get_attachment_metadata?
Because I cant get it to return it, and a var dump doesnt seem to show it:
array(5) {
["width"]=>
int(400)
["height"]=>
int(300)
["file"]=>
string(19) "2018/07/400x300.png"
["sizes"]=>
array(3) {
["thumbnail"]=>
array(4) {
["file"]=>
string(19) "400x300-150x150.png"
["width"]=>
int(150)
["height"]=>
int(150)
["mime-type"]=>
string(9) "image/png"
}
["medium"]=>
array(4) {
["file"]=>
string(19) "400x300-300x225.png"
["width"]=>
int(300)
["height"]=>
int(225)
["mime-type"]=>
string(9) "image/png"
}
["square"]=>
array(4) {
["file"]=>
string(19) "400x300-300x300.png"
["width"]=>
int(300)
["height"]=>
int(300)
["mime-type"]=>
string(9) "image/png"
}
}
["image_meta"]=>
array(12) {
["aperture"]=>
string(1) "0"
["credit"]=>
string(0) ""
["camera"]=>
string(0) ""
["caption"]=>
string(0) ""
["created_timestamp"]=>
string(1) "0"
["copyright"]=>
string(0) ""
["focal_length"]=>
string(1) "0"
["iso"]=>
string(1) "0"
["shutter_speed"]=>
string(1) "0"
["title"]=>
string(0) ""
["orientation"]=>
string(1) "0"
["keywords"]=>
array(0) {
}
}
}
Here is the code I tried:
<?php
$id = get_field('primary_image');
$img_meta = wp_get_attachment_metadata( $id );
echo $img_meta[image_meta][caption];
?>
Ok, after much gnashing of teeth and sighing I finally got something that worked. Immediately the problem is that wp_get_attachment_metadata is for something else. I wanted wp_get_attachment
Function:
function wp_get_attachment( $attachment_id ) {
$attachment = get_post( $attachment_id );
return array(
'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ),
'caption' => $attachment->post_excerpt,
'description' => $attachment->post_content,
'href' => get_permalink( $attachment->ID ),
'src' => $attachment->guid,
'title' => $attachment->post_title
);
}
Useage:
<?php
$attachment = wp_get_attachment( get_field( 'primary_image' ) );
echo $attachment['caption'];
?>
Thanks for posting your solution here @pierrebalian
You could also do this:
$img_acf = get_field('primary_image');
$img_acf_size = 'full';
$img_acf_src = wp_get_attachment_image_src( $img_acf, $img_acf_size );
$img_acf_caption = get_the_excerpt( $img_acf );
And then:
<?php echo $img_acf_caption; ?>
And because we love ACF so much we can give it some logic:
<img src="<?php echo esc_url( $img_acf_src[0] ); ?>">
<?php if( $img_acf_caption ){ ?>
<div><?php echo $img_acf_caption; ?></div>
<?php } ?>
The topic ‘Function to Return Image Caption from ID not Image Object?’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.