Display Featured Image with PHP Code in wordpress

Explore our WordPress category, where you’ll find loads of helpful articles and tutorials for everyone, whether you’re just starting out or a pro developer. Learn about different WordPress themes and plugins that can be applied in various projects according to their specific requirements.
This is a piece of PHP code that retrieves the URL of the featured image for a WordPress post and then displays it using an tag.
<?php $featured_img = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), ‘full’); ?>
<img src=”<?php echo $featured_img[0]; ?>” />
wp_get_attachment_image_src: This WordPress function is used to retrieve the image source (URL) for a given attachment or featured image.
get_post_thumbnail_id($post->ID): This function is used to get the attachment ID of the featured image for the current post ($post->ID).
‘full’: This is the size parameter for the image. It specifies that you want to retrieve the URL for the full-sized image. You can change this to other available sizes like ‘thumbnail’, ‘medium’, or other custom sizes depending on your needs.
$featured_img = wp_get_attachment_image_src(…);: This line stores the result of the wp_get_attachment_image_src function in the variable $featured_img. The result is an array where the image URL is at index 0.
<img src=”<?php echo $featured_img[0]; ?>” />: This line outputs the <img> tag with the src attribute set to the URL of the featured image. The PHP echo statement is used to insert the URL into the HTML.