How to Display The WordPress Featured Image Caption Without a Plugin

How To Display The WordPress Featured Image Caption Without A Plugin
How To Display The WordPress Featured Image Caption Without A Plugin

This WordPress code will allow you to display the image caption for the featured image in your WordPress posts without using a plugin.

Instructions
Add this code to your theme template (eg. single.php) where you are displaying the image.

<?php if (get_post( get_post_thumbnail_id())->post_content);  // check if image has caption added ?>
     <div class="featured_image_caption">
          <?php echo wp_kses_post(get_post( get_post_thumbnail_id() )->post_content ); // outputs image caption ?>
     </div>
<?php endif; ?>

Note: You can replace post_excerpt with post_content to get the image description etc.

Drop your comments below

Spread the love

You Might Also Like

Leave a Reply

Your email address will not be published.