[Support request] Caption in featured image inside page hero

Home Forums Support [Support request] Caption in featured image inside page hero

Home Forums Support Caption in featured image inside page hero

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #2057610
    Evenit

    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

    #2057879
    David
    Staff
    Customer Support

    Hi there,

    this topic i provide a PHP Snippet to create the shortcode to display the caption:

    https://generatepress.com/forums/topic/show-specific-text-if-fallback-image-is-used-instead-featured-image/#post-1686941

    #2058057
    Evenit

    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.

    #2058345
    David
    Staff
    Customer Support

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

    #2058653
    Evenit
    <?php
    $get_description = get_post(get_post_thumbnail_id())->post_excerpt;
      if(!empty($get_description)){
      echo '<div class="featured_caption">' . $get_description . '</div>';
      }
    ?>
    #2058689
    David
    Staff
    Customer Support

    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();
    } );
    #2060263
    Evenit

    Thanks David, it works perfect

    #2060360
    David
    Staff
    Customer Support

    Awesome – glad to be of help!

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