How to get the WordPress post thumbnail (featured image) URL?
Check the code below and let me know if it works for you.
<?php if (has_post_thumbnail( $post->ID ) ): ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<div id="custom-bg" style="background-image: url('<?php echo $image[0]; ?>')">
</div>
<?php endif; ?>
Wordpress Featured Image URL
please try this code adding in to your post loop. and confirm you have set feature image for that post which you listings.
<?php $post_thumbnail_id = get_post_thumbnail_id( $post->ID );
if(!empty($post_thumbnail_id)) {
$img_ar = wp_get_attachment_image_src( $post_thumbnail_id, 'full' ); ?>
<img src="<?php echo $img_ar[0];?>" />
<?php } ?>
How to get featured image from post
save_post
hook can be used for this purpose it has a third parameter
$update
which can be used to determine just to check it is new or
existing.
/**
* Save post metadata when a post is saved.
*
* @param int $post_id The post ID.
* @param post $post The post object.
* @param bool $update Whether this is an existing post being updated or not.
*/
function save_post_first_time($post_id, $post, $update)
{
//check for revision
if (wp_is_post_revision($post_id))
return;
if (!$update)
{
// it's a new post
//getting post featured image ID
$post_thumbnail_id = get_post_thumbnail_id($post_id);
$imageSRC = wp_get_attachment_image_src($post_thumbnail_id, 'thumbnail');
}
else
{
// it's an existing post
}
}
add_action('save_post', 'save_post_first_time', 10, 3);
Reference:
- save_post
- the_post_thumbnail_url
- get_post_thumbnail_id
Hope this helps!
How to get WordPress post featured image from post ID in url
Try this
<?php
add_shortcode('CF7_ADD_POST_ID', 'cf7_add_post_id');
function cf7_add_post_id(){
$ID = isset( $_GET["postid"] ) ? $_GET["postid"] : false;
if( $ID ){
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id( $ID ), 'full' );
$url = $thumb['0'];
echo "<img src ='".$url."' alt = 'Image'>";
}
}
?>
how to get url ONLY of featured image in wordpress
If your "thumbnail" images are large enough to do what you need:
<?php wp_get_attachment_url(get_post_thumbnail_id($post->ID)); ?>
Otherwise you'll need to go for something like:
<?php $src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), array(300, 300), false, ''); echo $src[0]; ?>
EDIT: The array(300, 300)
bit is the image size you'd like to fetch. WP will substitute any image it has that is at least as large as what you asked for. You can also use 'thumbnail'
, 'medium'
, 'large'
or 'full'
to select one of the pre-configured sizes, as well as any name defined by add_image_size()
in your template or plugins.
wordpress display featured image
Featured image is only one per post/page and you can get it using
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
This function directly prints the image (an img tag) and get_the_post_thumbnail()
function returns an image so you have to echo it. Your code and question is confusing because you can't have three featured images for a single post/page but can have multiple images attached to a post/page. if you are trying to get all images then you can check following articles.
- Display All Images Attached to a Post in WordPress.
- display all images attached to a post without a plugin.
Wordpress: Get featured image url of home page in another page
You have to use get_option to find the home page.
$home_id = get_option('page_on_front');
$home_thumb_id = get_post_thumbnail_id($home_id);
echo wp_get_attachment_image($home_thumb_id, 'large');
Wordpress: Get Featured Image URL from Database
The featured image is stored in the
wp_postmeta table with the meta_key _thumbnail_id
you can get it by
$Featured_image = $wpdb->get_results("
SELECT p.*
FROM net_5_postmeta AS pm
INNER JOIN net_5_posts AS p ON pm.meta_value=p.ID
WHERE pm.post_id = $da_id
AND pm.meta_key = '_thumbnail_id'
ORDER BY p.post_date DESC
LIMIT 15
",'ARRAY_A'
or
SELECT * from {$wpdb->prefix}_posts
WHERE ID in (
SELECT meta_value FROM {$wpdb->prefix}_postmeta
WHERE meta_key = '_thumbnail_id'
AND post_id = ':ID'
);
Replace ID by your post id
To Get the Post Thumbnail URL in WordPress
<?php
$thumb_id = get_post_thumbnail_id();
$thumb_url = wp_get_attachment_image_src($thumb_id,'thumbnail-size', true);
echo $thumb_url[0];
?>
for reference :URL
Get Featured Image of Pages - WordPress
You're close, but you should be using wp_get_attachment_image_src()
, and should remove the quotes from your variable when you echo
the source:
$feat_image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'large' );
<a href="<?php echo get_permalink( $page->ID ); ?>"
style="background-image: url(<?php echo $feat_image; ?>)">
You should also take a look at your HTML markup and make sure it's valid.
Related Topics
Auto Increment Skipping Numbers
Is There Any Difference Between _Dir_ and Dirname(_File_) in PHP
How to Make Short Random Unique Keys, Like Youtube Video Ids, in PHP
How to Create a .Gz File Using PHP
Phpmailer Character Encoding Issues
How to Get Wordpress Post Featured Image Url
Any Way to Break If Statement in PHP
Display an Array in a Readable/Hierarchical Format
Xdebug for Remote Server Not Connecting
Laravel Password Validation Rule
Get the Number of Pages in a PDF Document
Calling a Function Within a Class Method
Laravel Validation Attributes "Nice Names"