Advertisements

set_post_thumbnail

Definition:
function set_post_thumbnail( $post, $thumbnail_id ) {}

Sets a post thumbnail.

Parameters

  • int|object $post: Post ID or object where thumbnail should be attached.
  • int $thumbnail_id: Thumbnail to attach.

Return values

returns:True on success, false on failure.

Source code

function set_post_thumbnail( $post, $thumbnail_id ) {

	$post = get_post( $post );

	$thumbnail_id = absint( $thumbnail_id );

	if ( $post && $thumbnail_id && get_post( $thumbnail_id ) ) {

		$thumbnail_html = wp_get_attachment_image( $thumbnail_id, 'thumbnail' );

		if ( ! empty( $thumbnail_html ) ) {

			update_post_meta( $post->ID, '_thumbnail_id', $thumbnail_id );

			return true;

		}

	}

	return false;

}

10273

Advertisements

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: