WordPress.org

Ready to get started?Download WordPress

Codex

Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/get post thumbnail id

Contents

Description

  1. If a featured image (formerly known as post thumbnail) is set - Returns the ID of the featured image attached to the post
  2. If no such attachment exists, the function returns an empty string
  3. If the post does not exist, the function returns false

Note: To enable featured images, see post thumbnails, the current theme must include add_theme_support( 'post-thumbnails' ); in its functions.php file. See also Post Thumbnails.

Usage

 <?php  $post_thumbnail_id get_post_thumbnail_id$post_id ); ?> 

Parameters

$post_id
(integer) (Optional) Post ID. If null, the current post will be used.
Default: null

Return Values

(string) 
The ID of the post, or an empty string on failure.

Examples

Show all attachments for the current post except the Featured Image

To get all post attachments except the Featured Image, you can use this function with something like get_posts().

Do this inside The_Loop (where $post->ID is available).

<?php

$args = array(
	'post_type'   => 'attachment',
	'numberposts' => -1,
	'post_status' => 'any',
	'post_parent' => $post->ID,
	'exclude'     => get_post_thumbnail_id()
	);

$attachments = get_posts( $args );

if ( $attachments ) {
	foreach ( $attachments as $attachment ) {
		echo apply_filters( 'the_title', $attachment->post_title );
		the_attachment_link( $attachment->ID, false );
	}
}

?>

Notes

  • "Post Thumbnail" is an outdated term for "Featured Image". This function returns the ID of the post's featured image. It does not return IDs of other images attached to posts that are thumbnail sized.

Change Log

Since: 2.9.0

Source File

get_post_thumbnail_id() is located in wp-includes/post-thumbnail-template.php.

Related

Post Thumbnails: has_post_thumbnail(), the_post_thumbnail(), get_post_thumbnail_id(), get_the_post_thumbnail(), add_image_size(), set_post_thumbnail_size()

See also index of Function Reference and index of Template Tags.