rugbert
rugbert

Reputation: 12653

How do I get the featured image description from my wordpress page?

So I set up my wordpress theme to allow users to upload featured images, and Im building my index page to display selected pages' featured images but would also like to display the description of the image.

The thing is, Im not using the loop, Im pulling the page IDs using wordpress's settings API as options.

So displaying the featured images is done like this:

<?php $bucket_options = get_option('frontpage_display_options'); ?>
<?php $page_one = $bucket_options['frontpage_bucket_one']; ?>
<?php $page_one = get_post($page_one);  ?>
<?php if (has_post_thumbnail($page_one->ID)) : ?>  
      <?php echo get_the_post_thumbnail($page_one->ID, 'bucket'); ?>  
<?php endif; ?>

I keep reading that this will work:

echo get_post(get_the_post_thumbnail_id($page_one->ID))->post_content;

or this:

echo get_post(get_the_post_thumbnail($page_one->ID))->post_content;

But neither of them displays anything

Upvotes: 4

Views: 7947

Answers (2)

Fredy Peralta
Fredy Peralta

Reputation: 21

This works for me. It echo's the title, caption and description of the featured image.

<?php
if ( has_post_thumbnail() ) :
the_post_thumbnail();
echo '<p>' . get_post(get_post_thumbnail_id())->post_title . '</p>';
echo '<p>' . get_post(get_post_thumbnail_id())->post_excerpt . '</p>';
echo '<p>' . get_post(get_post_thumbnail_id())->post_content . '</p>';
endif;
?>

Upvotes: 2

markratledge
markratledge

Reputation: 17561

That capability is awaiting a new release: http://core.trac.wordpress.org/ticket/12235

But a solution that is floating around is to create a function in functions.php:

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>';
  }
}

And then call the_post_thumbnail_caption();

Upvotes: 7

Related Questions