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

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…

2 weeks ago

10 Smart Strategies for a Secure and Enjoyable Retirement

Retiring comfortably is a goal many aspire to achieve, and with the right strategies, it’s…

2 weeks ago

Troubleshooting a Slow Computer

Is your computer running frustratingly slow? Don't worry; you're not alone in facing this common…

2 weeks ago

The Clever Fox and the Greedy Crow

Once, in a sunny forest, Fox and Crow were friends. One day, Crow found a…

2 weeks ago

The Little Lantern and the Lost Stars

Once upon a time, in a cozy village nestled between rolling hills and whispering forests,…

2 weeks ago