Archived topic
Dynamic featured image caption
3 replies · Started by Jayanta on April 12, 2022
Hi,
I was trying to create a Block element to define “content template” for all posts. In my previous design, the display is in this hierarchy: Headline>Post meta (author + date)>Featured Image>Featured image caption>post body.
While I am able to create the new Block element with dynamic headline, dynamic post meta, dynamic featured image and the dynamic post body, I am unable to insert the Featured Image caption.
I tried using Dynamic headline with Dynamic Text Type set to Post Meta and Meta Field Name as featured_caption. However, this did not yield any result as I don’t use any custom fields to insert the caption. I use the default image-caption option when uploading.
Please help.
Hi there,
Try this solution:
1. Add this PHP code to your site to create a shortcode [featured_img_caption]:
add_shortcode( 'featured_img_caption', function() {
ob_start();
echo '<div>'. get_post(get_post_thumbnail_id())->post_excerpt. '</div>';
return ob_get_clean();
} );
Adding PHP: https://docs.generatepress.com/article/adding-php/
2. In your content template, add a shortcode block with this shortcode: [featured_img_caption] anywhere you want.
If you want to style the text, it's better wrapper the shortcode block with a GB container block, then you can set the style for the GB contianer.
Thnk you so much Ying. This worked perfectly.
You are welcome :)