Archived topic
Display image caption on the featured image
12 replies · Started by Simon on February 3, 2019
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.
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?
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)"
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.
You only need to add that code once.
Follow the instructions provided here:
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
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.
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.