【WordPress】アイキャッチ画像URLの取得【WP4.4〜】
the_post_thumbnail_url
// アイキャッチ画像のURL文字列を出力する
the_post_thumbnail_url( $size );
get_the_post_thumbnail_url( get_the_ID(), $size );
/**
* Return the post thumbnail URL.
*
* @since 4.4.0
*
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
* @param string|array $size Optional. Registered image size to retrieve the source for or a flat
* array of height and width dimensions. Default 'post-thumbnail'.
* @return string|false Post thumbnail URL or false if no URL is available.
*/
function get_the_post_thumbnail_url( $post = null, $size = 'post-thumbnail' ) {
$post_thumbnail_id = get_post_thumbnail_id( $post );
if ( ! $post_thumbnail_id ) {
return false;
}
return wp_get_attachment_image_url( $post_thumbnail_id, $size );
}
/**
* Display the post thumbnail URL.
*
* @since 4.4.0
*
* @param string|array $size Optional. Image size to use. Accepts any valid image size,
* or an array of width and height values in pixels (in that order).
* Default 'post-thumbnail'.
*/
function the_post_thumbnail_url( $size = 'post-thumbnail' ) {
$url = get_the_post_thumbnail_url( null, $size );
if ( $url ) {
echo esc_url( $url );
}
}