Site logo

Archived topic

Display image caption on the featured image

12 replies · Started by Simon on February 3, 2019

Viewing posts 1–13 of 13

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.

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.

The php execution was disabled on my server.

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

Thanks for the support.

Great to hear. Glad to be of help.

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.

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.

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

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

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.

This archived topic is closed to new replies.