wpseek.com
Bazujące na WordPress narzędzie wyszukiwania dla deweloperów i twórców motywów.



set_post_thumbnail › WordPress Function

Od3.1.0
Przestarzałyn/a
› set_post_thumbnail ( $post, $thumbnail_id )
Parametry: (2)
  • (int|WP_Post) $post Post ID or post object where thumbnail should be attached.
    Wymagane: Tak
  • (int) $thumbnail_id Thumbnail to attach.
    Wymagane: Tak
Powrót:
  • (int|bool) Post meta ID if the key didn't exist (ie. this is the first time that
    a thumbnail has been saved for the post), true on successful update,
    false on failure or if the value passed is the same as the one that
    is already in the database.
Zdefiniowane na:
Codex:

Sets the post thumbnail (featured image) for the given post.



Źródło

function set_post_thumbnail( $post, $thumbnail_id ) {
	$post         = get_post( $post );
	$thumbnail_id = absint( $thumbnail_id );
	if ( $post && $thumbnail_id && get_post( $thumbnail_id ) ) {
		if ( wp_get_attachment_image( $thumbnail_id, 'thumbnail' ) ) {
			return update_post_meta( $post->ID, '_thumbnail_id', $thumbnail_id );
		} else {
			return delete_post_meta( $post->ID, '_thumbnail_id' );
		}
	}
	return false;
}