How to get post image caption by index
Question
I have multiple images in WordPress post, each image has different caption.
I want to get the image caption by the image index, but I can’t figure out how to print the caption.
Here is what I have tried:
//$index - index of the image (if index is 0, its first image)
function getImageCaption($postID, $index){
$post_id = $postID;
// get the post object
$post = get_post( $post_id );
// get the post thumbnail ID
$thumbnail_id = get_post_thumbnail_id($post->ID);
// we need just the content
$thumbnail_image = get_posts(array('p' => $thumbnail_id, 'post_type' => 'attachment'));
if ($thumbnail_image && isset($thumbnail_image[$index])) {
return $thumbnail_image[$index]->post_excerpt;
} else {
return;
}
}
How can I display image caption by index?
0
2 months
0 Answers
14 views
0
Leave an answer
You must login or register to add a new answer .