[Resolved] Dynamic featured image caption

Home Forums Support [Resolved] Dynamic featured image caption

Home Forums Support Dynamic featured image caption

  • This topic has 3 replies, 2 voices, and was last updated 2 years ago by Ying.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #2186728
    Jayanta

    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. caption
    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.

    #2186862
    Ying
    Staff
    Customer Support

    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.

    #2186991
    Jayanta

    Thnk you so much Ying. This worked perfectly.

    #2187882
    Ying
    Staff
    Customer Support

    You are welcome 🙂

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.