Archived topic
Display caption on featured image
15 replies · Started by Capital Link on November 8, 2019
How can I display the caption of featured image from the default wp posts?
I install the https://wordpress.org/plugins/featured-image-caption/ and I can see the caption text that I add on that fields of this plugin.
Hi there,
is this to be displayed on the Single Post or the Blog / Archives?
Yes, if I have on the functions.php the
add_action( 'generate_before_content', 'generate_post_image_caption' );
function generate_post_image_caption()
{
// If there's no featured image, return
if ( ! has_post_thumbnail() )
return;
//Modified. This is checking for a post that IS a single post and NOT a 404
if ( is_singular() && ! is_404()) {
?>
<?php if ( function_exists( 'cc_featured_image_caption' ) ) : ?>
<?php cc_featured_image_caption(); ?><?php endif; ?>
<?php
}
}
from https://generatepress.com/forums/topic/caption-for-post-thumbnails/page/2/
P.S. The captions is displayed on top of featured images instead of bottom.
Hi there,
instead of using the plugin and that filter you can use the Code i provide here:
https://generatepress.com/forums/topic/cant-display-featured-image-captions/#post-854633
Will need to see the site once its in place to help with any styling.
Where do I have to paste the provided php code?
In functions.php?
If so I paste it and nothing happened :(
Yes - it goes in functions.php or the code snippets plugin.
Can you leave the code in place and link me to the site so i can take a look?
Ok I add that code.
How can i give you access?
The site is not public.
Can you give me your e-mail?
You can see URL and login details via the Accounts Issue form:
https://generatepress.com/contact/
Please add the URL for this topic to the form so we can track
Added!
It was also nice to have ticket number when we send issues - credentials etc.
Did you resize the image in the Customizer > Layout > Blog ?
If so can you clear the sizes - just delete the numbers and hit apply.
The caption text displayed at the home page but not in the post.
Do I have to change anything else?
I use child theme.
In the Customizer > Layout > Blog under Featured images you can change the size for Posts, Pages and Archives, can you make sure the Post is set to Auto by removing any sizes in there.
I see now the caption but only if the size of the image is set to Auto.
If I set 500 px I cannot see the caption. Why?
Resizing the image changes the function used to display the image.
Try this snippet instead:
add_filter( 'generate_single_featured_image_html', 'custom_add_post_thumbnail_caption',25,1 );
function custom_add_post_thumbnail_caption($image_html) {
$get_caption = get_post(get_post_thumbnail_id())->post_excerpt;
if( $image_html == '' && empty($get_caption)) {
return $image_html;
} else {
$caption = '<div class="wp-caption thumb-caption">' . $image_html . '<p class="wp-caption-text thumb-caption-text">' . $get_caption .'</p></div>';
}
return $caption;
}
Thank you
This is working very nice now.