Categories
PHP WordPress

Display caption with post thumbnail in WordPress

 

<figure>
    <?php 
    the_post_thumbnail();
    $caption = get_post( get_post_thumbnail_id() )->post_excerpt;

    if ( $caption != "" ): ?>
        <figcaption>
            <?php echo $caption; ?>
        </figcaption>
    <?php endif; ?>
</figure>

2 replies on “Display caption with post thumbnail in WordPress”

Morten I updated the post. Caption is displayed only if it is not empty.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.