Categories: wordpress

HOW TO DISPLAY ALT IMAGE AND CAPTION OF IMAGE IN WORDPRESS THEME DEVELOPMENT?

Alt is crucial part of SEO because it describe the image for engines and screen readers. whereas, the caption is optional and can be used only when you need to provide additional information about the image to website visitors.

FOR CAPTION
to display caption is an easy task. just you need to put this code where needed ,
the_post_thumbnail_caption();

If you want to displey only if the caption is available then you can do the following code.
if(the_post_thumbnail_caption())
{
the_post_thumbnail_caption();
}

FOR ALT
To display image alt in three section.
1st: Get Thumbnail ID
You can get thumnail id by get_post_thumbnail_id(get_the_ID())
2nd: get alt by help of id
get_post_meta($thumb_id,’_wp_attachment_image_alt’,true);
3rd: display the alt if exist
if(count($alt)) {echo $alt; }

Combining all you can write in this method

$thumb_id=get_post_thumbnail_id(get_the_ID());
$alt= get_post_meta( $thumb_id, ‘_wp_attachment_image_alt’, true );
if(count($alt)) {echo $alt; } 

jaminrai

Recent Posts

Computer Science Project Report Guidelines and Sample

Guidelines (Click Below to Download) SAMPLE 1 SAMPLE 2

5 months ago

Skype Closes Its Doors After 20 Years: A Look Back and the Apps That Faded with It

After 20 years of transforming global communication, Skype has officially ceased operations. Microsoft, which purchased…

12 months ago

The Duck and the Tortoise: A Tale of Patience and Teamwork

In a serene forest by the edge of a sparkling lake, there lived a cheerful…

1 year ago