Site logo

Archived topic

How to add caption in featured image

1 reply · Started by Vivek on January 22, 2023

Viewing posts 1–2 of 2

Hi
I want to add caption in the featured image
i tried looking to other topics and used this code

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 == '' ) {

return $html;

} else {

$out = '';

$thumbnail_image = get_posts(array('p' => $post_thumbnail_id, 'post_type' => 'attachment'));

if ($thumbnail_image && isset($thumbnail_image[0])) {

$image = wp_get_attachment_image_src($post_thumbnail_id, $size);

if($thumbnail_image[0]->post_excerpt)
$out .= '

';

$out .= $html;

if($thumbnail_image[0]->post_excerpt)
$out .= '<p class="wp-caption-text thumb-caption-text">'.$thumbnail_image[0]->post_excerpt.'</p>

';

}

return $out;

}
}

but still the caption is not in a perfect position

Here is the link of the screenshot:
https://ibb.co/dbpcrrx
https://ibb.co/jb2ySd4

Please reply to my other queries

Hi there,

can you share a link to the site?
You can use the Private Information field to share URLs and login details

This archived topic is closed to new replies.