Что возвращает метод the_post_thumbnail() в wordpress если указать изображение которое не существует?
Пробовал функции: empty, isset, is_null.
if(the_post_thumbnail() === NULL) {
echo 'Данных нет!';
}
Ничего - выводит на экран строку. Функция выводит на экран html код картинки или пустое значение (null), если картинки не существует.
Уточнение: the_post_thumbnail() это не метод класса, а функция. Вот ее код:
function the_post_thumbnail( $size = 'post-thumbnail', $attr = '' ) {
echo get_the_post_thumbnail( null, $size, $attr );
}
Вот код get_the_post_thumbnail():
function get_the_post_thumbnail( $post = null, $size = 'post-thumbnail', $attr = '' ) {
$post = get_post( $post );
if ( ! $post ) {
return '';
}
$post_thumbnail_id = get_post_thumbnail_id( $post );
/**
* Filters the post thumbnail size.
*
* @since 2.9.0
*
* @param string|array $size The post thumbnail size. Image size or array of width and height
* values (in that order). Default 'post-thumbnail'.
*/
$size = apply_filters( 'post_thumbnail_size', $size );
if ( $post_thumbnail_id ) {
/**
* Fires before fetching the post thumbnail HTML.
*
* Provides "just in time" filtering of all filters in wp_get_attachment_image().
*
* @since 2.9.0
*
* @param int $post_id The post ID.
* @param string $post_thumbnail_id The post thumbnail ID.
* @param string|array $size The post thumbnail size. Image size or array of width
* and height values (in that order). Default 'post-thumbnail'.
*/
do_action( 'begin_fetch_post_thumbnail_html', $post->ID, $post_thumbnail_id, $size );
if ( in_the_loop() )
update_post_thumbnail_cache();
$html = wp_get_attachment_image( $post_thumbnail_id, $size, false, $attr );
/**
* Fires after fetching the post thumbnail HTML.
*
* @since 2.9.0
*
* @param int $post_id The post ID.
* @param string $post_thumbnail_id The post thumbnail ID.
* @param string|array $size The post thumbnail size. Image size or array of width
* and height values (in that order). Default 'post-thumbnail'.
*/
do_action( 'end_fetch_post_thumbnail_html', $post->ID, $post_thumbnail_id, $size );
} else {
$html = '';
}
/**
* Filters the post thumbnail HTML.
*
* @since 2.9.0
*
* @param string $html The post thumbnail HTML.
* @param int $post_id The post ID.
* @param string $post_thumbnail_id The post thumbnail ID.
* @param string|array $size The post thumbnail size. Image size or array of width and height
* values (in that order). Default 'post-thumbnail'.
* @param string $attr Query string of attributes.
*/
return apply_filters( 'post_thumbnail_html', $html, $post->ID, $post_thumbnail_id, $size, $attr );
}
Как видим, если не найден пост, или у него нет изображения, функция возвращает пустую строку, которая потом и выводится на экран.
Сама функция the_post_thumbnail() не возвращает ничего.
Проблема решена.
Проверку делаем через has_post_thumbnail() возвращает true если миниатюра есть то-есть код такой.
if(has_post_thumbnail())
{
the_post_thumbnail();
}
else
{
echo 'Данных нет!';
}
PS: Через другие способы результат был не очень хорош
Продвижение своими сайтами как стратегия роста и независимости