Featured image captions in WordPress allow to add descriptive text or additional information to accompany the featured image of a post or page. This caption can provide context or give credit to the image source. To show captions in all featured images in WordPress, you can modify your theme’s code by following these steps:
Step 1. Open the functions.php file and add the following code at the end:
function text_domain_show_featured_image_caption() { $caption = get_the_post_thumbnail_caption(); if ($caption) { return '<div class="image-caption">' . $caption . '</div>'; } }
Step 2. Now, you can display the caption alongside the featured image by calling the text_domain_show_featured_image_caption() function in the appropriate template files. Locate the template file responsible for displaying featured images (e.g., single.php, content.php), and within the loop where the featured image is displayed, add the following code:
<?php echo text_domain_show_featured_image_caption(); ?>
The code usually looks like this:
<?php the_post_thumbnail(); echo text_domain_show_featured_image_caption(); ?>
Thanks! Happy coding.
Leave a Reply