Site logo

Archived topic

Caption in featured image inside page hero

7 replies · Started by Evenit on December 23, 2021

Viewing posts 1–8 of 8

Hi, I would like to show caption for featured image inside the Page Hero.
Page Hero code is:

<h1>{{post_title}}</h1>	
<div class="mypageherometa"> 
<span class="updated">[modified_date]</span> /
 {{post_author}}
</div>
<div id="sfondoimghero">
[featured_image]
</div>

May you tell how can I add caption inside the page hero, after [featured_image] ?
Thanks

Thank you David but does not work for me. It generates the code <p class="featured-caption"></p> but is empty...
maybe I'm doing something wrong?
Please check the header element of this page: https://www.viaggi-usa.it/visitare-providence/
PS: don't take into account the caption below the Header Element, I have inserted it with php code with a hook.

Thats peculiar.... what PHP are you using in the working Hook below the image ?

<?php
$get_description = get_post(get_post_thumbnail_id())->post_excerpt;
  if(!empty($get_description)){
  echo '<div class="featured_caption">' . $get_description . '</div>';
  }
?>

Aah it's the description.
Remove the PHP for the shortcode and add this instead:

add_shortcode( 'featured_caption', function() {
    ob_start();

    $get_description = get_post(get_post_thumbnail_id())->post_excerpt;
    if( !empty($get_description) ){
        echo '<div class="featured_caption">' . $get_description . '</div>'; 
    }  

    return ob_get_clean();
} );

Thanks David, it works perfect

Awesome - glad to be of help!

This archived topic is closed to new replies.