Archived topic
Add featured image caption via Elements
9 replies · Started by JC on April 29, 2021
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 there,
you would need to create a shortcode to display the caption, which can then be added using Elements - heres an example which also includes a fallback text if there is no image caption set:
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?
Hi there,
You can use this filter to modify the featured image to add your caption.
https://docs.generatepress.com/article/generate_featured_image_output/
You can either insert a do_shortcode() in it containing your created shortcode or code something within the filter.
Hi Elvin,
It's safe to assume that I am not proficient when it comes to PHP but of all the solutions this one ended up working for me;
https://generatepress.com/forums/topic/featured-image-badge/
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