acf_get_post_thumbnail()
acf_get_post_thumbnail
This function will return a thumbail image url for a given post
Хуков нет.
Возвращает
(Строку).
Использование
acf_get_post_thumbnail( $post, $size );
- $post
- .
По умолчанию: null - $size
- .
По умолчанию: 'thumbnail'
Список изменений
| С версии 5.3.8 | Введена. |
Код acf_get_post_thumbnail() acf get post thumbnail ACF 6.4.2
function acf_get_post_thumbnail( $post = null, $size = 'thumbnail' ) {
// vars
$data = array(
'url' => '',
'type' => '',
'html' => '',
);
// post
$post = get_post( $post );
// bail early if no post
if ( ! $post ) {
return $data;
}
// vars
$thumb_id = $post->ID;
$mime_type = acf_maybe_get( explode( '/', $post->post_mime_type ), 0 );
// attachment
if ( $post->post_type === 'attachment' ) {
// change $thumb_id
if ( $mime_type === 'audio' || $mime_type === 'video' ) {
$thumb_id = get_post_thumbnail_id( $post->ID );
}
// post
} else {
$thumb_id = get_post_thumbnail_id( $post->ID );
}
// try url
$data['url'] = wp_get_attachment_image_src( $thumb_id, $size );
$data['url'] = acf_maybe_get( $data['url'], 0 );
// default icon
if ( ! $data['url'] && $post->post_type === 'attachment' ) {
$data['url'] = wp_mime_type_icon( $post->ID );
$data['type'] = 'icon';
}
// html
$data['html'] = '<img src="' . $data['url'] . '" alt="" />';
// return
return $data;
}