Site logo

Archived topic

Featured Image Caption NOT showing

5 replies · Started by Alejandro on July 23, 2020

Viewing posts 1–6 of 6

Hi,
I want to see the featured image caption.
To achieve that I set up a Hook as you describe in this post

I set it after_entry_header but it shows just after_entry_title.
I tried to switch where the code hooks (tried after_entry_title as well) to check if there is any function swapping but it stills show after_entry_title....

Any help please?

Thanks David, it works pretty fine.
I am only interested in showing it in the single post, not on the blog roll or elsewhere. I am using WP Show Posts PRO and I don't want to show the caption through it.
What should I change then?

Thanks!

try this code instead:

add_filter( 'post_thumbnail_html', 'custom_add_post_thumbnail_caption',10,5 );
 
function custom_add_post_thumbnail_caption($html, $post_id, $post_thumbnail_id, $size, $attr) {

    if ( !$html == '' && is_single() ) {
        // Get Excerpt of thumbnail
        $thumbnail_image = get_posts(array('p' => $post_thumbnail_id, 'post_type' => 'attachment'));
        $thumbnail_caption = $thumbnail_image[0]->post_excerpt;

        if ( $thumbnail_caption ) {
            $html .= '<div class="wp-caption thumb-caption">
            <p class="wp-caption-text thumb-caption-text">'. $thumbnail_caption .'</p>
            </div>';
        }
    }
    return $html;
}

You made it!

Works flawlessly. Thanks for the help.

Awesome - glad to be of help

This archived topic is closed to new replies.