Site logo

Archived topic

Display caption on featured image

15 replies · Started by Capital Link on November 8, 2019

Viewing posts 1–15 of 16

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.

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?

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.

This archived topic is closed to new replies.