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 send an email using PHP in WordPress?
- How to disable PHP warnings in WordPress?
- How to create a snippet in WordPress using PHP?
- How to get the site URL in WordPress using PHP?
- How to add a PHP header in WordPress?
- How to configure Nginx for WordPress?
- How to hide PHP warnings in WordPress?
- How to get post meta in WordPress using PHP?
- How to get categories in WordPress using PHP?
- How to get the post ID in WordPress using PHP?
See more codes...