php-wordpressHow to get the featured image in WordPress using PHP?
Getting the featured image in WordPress using PHP is a simple process. The following example code block will return the featured image URL of the current post:
<?php
$thumb_id = get_post_thumbnail_id();
$thumb_url = wp_get_attachment_image_src($thumb_id,'thumbnail-size', true);
echo $thumb_url[0];
?>
The output of the example code will be the URL of the featured image:
http://example.com/wp-content/uploads/featured-image.jpg
Code explanation
get_post_thumbnail_id()
- This function returns the post thumbnail ID.wp_get_attachment_image_src()
- This function returns an array of the image URL, width, height, and whether the image is an intermediate size.$thumb_url[0]
- This returns the URL of the featured image from the array.
Helpful links
More of Php Wordpress
- How to disable PHP warnings in WordPress?
- How to check the PHP version in WordPress?
- How to use get_header in WordPress using PHP?
- How to send an email using PHP in WordPress?
- How to increase the upload limit in WordPress using PHP?
- How to create a menu in WordPress using PHP?
- How to create a snippet in WordPress using PHP?
- How to redirect to another page in WordPress using PHP?
- How to use the WordPress REST API with PHP?
- How to get the current URL in WordPress using PHP?
See more codes...