Skip to content

Instantly share code, notes, and snippets.

@aurooba
Created May 31, 2016 15:37
Show Gist options
  • Save aurooba/fa72f9fcd4b29a623cc171e822e38dda to your computer and use it in GitHub Desktop.
Save aurooba/fa72f9fcd4b29a623cc171e822e38dda to your computer and use it in GitHub Desktop.
Post Thumbnail (Featured Image) Captions in WordPress
/**
* Check if Post Thumbnail (Featured Image) has a caption, return true if it does
*/
function has_post_thumbnail_caption() {
global $post;
$thumbnail_id = get_post_thumbnail_id($post->ID);
$thumbnail_image = get_posts(array('p' => $thumbnail_id, 'post_type' => 'attachment'));
$thumbnail_excerpt = $thumbnail_image[0]->post_excerpt;
if ($thumbnail_excerpt) {
return true;
} else {
return false;
}
}
/**
* Display Post Thumbnail (Featured Image) Caption
*/
function the_post_thumbnail_caption() {
global $post;
$thumbnail_id = get_post_thumbnail_id($post->ID);
$thumbnail_image = get_posts(array('p' => $thumbnail_id, 'post_type' => 'attachment'));
if ($thumbnail_image && isset($thumbnail_image[0])) {
echo '<span>'.$thumbnail_image[0]->post_excerpt.'</span>';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment