Site logo

Archived topic

Add featured image caption via Elements

9 replies · Started by JC on April 29, 2021

Viewing posts 1–10 of 10

To display credits I need to show image caption below the image on post pages.

If you can help point me in the right direction and possibly that I can do it via elements. The 2 solutions on the forum does not include how to via elements and also the one that did work added it after the post title and not the image so I must have used the wrong hook for it.

Thank you in advance :-)

Hi David,

Thank you for the quick response.

Ok, so if I do a shortcode might as well add it to the functions.php only so how do I get it to display after the featured image (below it) by adding it to the functions.php?

Have you sorted this out? The link you've added points to a search results list.

Let us know if you need further help. :)

I've updated the link to the correct one, https://generatepress.com/forums/topic/featured-image-badge/

The only problem is that displays the description and not the caption so how do I modify the below to display the caption and not the description from the image?

add_action( 'post_thumbnail_html', 'db_auto_single_featured_description', 10 );
function db_auto_single_featured_description( $html ) {
    $get_description = get_post(get_post_thumbnail_id())->post_excerpt;
    if ( is_single() && !empty($get_description) ){
      $html .= '<div class="wp-caption">'. $get_description . '</div>';
    }
    return $html;
}

Using this snippet for the Caption:

add_action( 'post_thumbnail_html', 'db_auto_single_featured_caption', 10 );
function db_auto_single_featured_caption( $html ) {
    $caption = get_the_post_thumbnail_caption();
    if ( is_single() && !empty($caption) ){
      $html .= '<div class="wp-caption">'. $caption . '</div>';
    }
    return $html;
}

And a little bit off CSS to style it:

.featured-image .wp-caption {
    line-height: 1.5em;
    font-size: 12px;
    text-align: left;
}

Thank you David! You are a superstar as always!

Glad to be of help

This archived topic is closed to new replies.