[Resolved] Display image caption on the featured image

Home Forums Support [Resolved] Display image caption on the featured image

Home Forums Support Display image caption on the featured image

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #800591
    Simon

    Hello,

    I would like to display the image caption automatically on the articles, I use the unsplash site and I should display the credits on the images used.

    I would like to display them just above the content or above the widget bar, how can I do that?

    I tried the shared codes on this post: https://generatepress.com/forums/topic/caption-for-post-thumbnails/ but none of them work.

    Sorry about my English, I use a translator.
    Thank you.

    #800893
    David
    Staff
    Customer Support

    Hi there,

    you can use the Hook Element:

    https://docs.generatepress.com/article/hooks-element-overview/

    Add this snippet:

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

    Choose which Hook you want to use:
    https://docs.generatepress.com/article/hooks-visual-guide/#posts-page

    Check Execute PHP

    And set your Display Rules for where the featured images are being used.

    #801100
    Simon

    The php execution was disabled on my server.

    I hadn’t seen the hooks page, it works perfectly:)

    Thanks for the support.

    #801126
    David
    Staff
    Customer Support

    Great to hear. Glad to be of help.

    #1505898
    Sagar

    I want to show featured image caption on all my blog posts.

    https://docs.generatepress.com/article/hooks-visual-guide/#single-post

    I want to show the caption after the featured image, not before that in the single blog posts.

    How to do that?

    #1505900
    Sagar

    Like this news website has given
    https://www.hindustantimes.com/it-s-viral/dad-s-complete-guide-to-baby-video-is-absolutely-hilarious-watch/story-opDXsEccg885ZFQXLnWooL.html

    The caption is: “The image shows a shot from the video. (Instagram/@howtodadnz)”

    #1506029
    David
    Staff
    Customer Support

    Hi there,

    add this PHP Snippet:

    add_action( 'post_thumbnail_html', 'tu_custom_byline', 10 );
    function tu_custom_byline( $html ) {
        if ( is_single() ){
          $html .= '<div class="wp-caption">Your Caption Here</div>';
        }
        return $html;
    }

    https://docs.generatepress.com/article/adding-php/

    Then some CSS to style it:

    .featured-image .wp-caption {
      padding: 10px 0;
      text-align: left;
    }

    If you need any further assistance, please raise a new topic where you can share a link to your site.

    #1506114
    Sagar

    Hi,

    This code will show the caption in all of my blog posts? Or do I need to enter this code on each blog post or something?

    I want if I enter the caption in any of the blogs featured images, it should come automatically without entering the code every time.

    Please guide me.

    #1506237
    David
    Staff
    Customer Support

    You only need to add that code once.
    Follow the instructions provided here:

    https://docs.generatepress.com/article/adding-php/

    #1506601
    Sagar

    I don’t want to use any plugin. I’m doing that through hook element > enabled Execute PHP with the above code you have given and generate_after_entry_header hook. Still not working

    #1506689
    Leo
    Staff
    Customer Support

    David’s code would only work if you add it using one of these methods:
    Adding PHP: https://docs.generatepress.com/article/adding-php/

    Code snippets plugin is the easiest way and should have minimal impact on your site – we use it all the time.

    The only other option is to use a child theme and add the code in the function.php file:
    https://docs.generatepress.com/article/using-child-theme/

    If you need more help with this, please open a new topic.

    #1506827
    Sagar

    Then what is the use of having this hook element? Why don’t we add it through that?

    #1506837
    Leo
    Staff
    Customer Support

    Hooks allow you to add content in specific spots (hooks) throughout the theme.

    post_thumbnail_html is not a hook.

    Please open a new topic if you need further help with this.

    Thank you.

Viewing 13 posts - 1 through 13 (of 13 total)
  • The topic ‘Display image caption on the featured image’ is closed to new replies.