The get_post_thumbnail_id()
function in WordPress is used to retrieve the post thumbnail (featured image) ID for a given post. This function takes a post ID as a parameter and returns the ID of the featured image associated with that post.
Here's how you can use get_post_thumbnail_id()
:
<?php
$post_id = 123; // Replace with the actual post ID
// Get the featured image ID for the post
$thumbnail_id = get_post_thumbnail_id($post_id);
// Check if a featured image is set for the post
if ($thumbnail_id) {
echo 'The featured image ID for post ' . $post_id . ' is: ' . $thumbnail_id;
} else {
echo 'No featured image set for post ' . $post_id;
}
?>
Replace 123
with the actual post ID for which you want to retrieve the featured image ID. If the post has a featured image, get_post_thumbnail_id()
will return the ID of that image. If there's no featured image set for the post, it will return null
.