has_post_thumbnail() WP 2.9.0
Проверяет имеет ли указанная запись (пост) картинку миниатюру. Условный тег.
Заметка! Чтобы для поста можно было определить картинку миниатюру, нужно активировать эту возможность функцией — add_theme_support( 'post-thumbnails' );
в файле шаблона funсtions.php.
Работает на основе: get_post_thumbnail_id()
1 раз = 0.000032с = очень быстро | 50000 раз = 0.47с = очень быстро | PHP 7.0.5, WP 4.5.2
Хуки из функции
Возвращает
true/false. true - если у поста есть миниатюра. false - если миниатюры нет.
Использование
<?php has_post_thumbnail( $post_id ); ?>
- $post_id(число/WP_Post)
- ID или объект поста, наличие миниатюры для которого нужно проверить.
По умолчанию: ID текущего поста в цикле.
Примеры
#1. Этот пример проверяет есть ли у поста прикрепленная к нему картинка миниатюра.
Если картинки не существует, то выведем картинку по умолчанию (определим её сами: загрузим файл в папку images нашей темы).
<?php //должно находится внутри цикла if( has_post_thumbnail() ) { the_post_thumbnail(); } else { echo '<img src="'.get_bloginfo("template_url").'/images/img-default.png" />'; } ?>
Список изменений
С версии 2.9.0 | Введена. |
С версии 4.4.0 | $post can be a post ID or WP_Post object. |
Код has_post_thumbnail() has post thumbnail WP 5.6
function has_post_thumbnail( $post = null ) {
$thumbnail_id = get_post_thumbnail_id( $post );
$has_thumbnail = (bool) $thumbnail_id;
/**
* Filters whether a post has a post thumbnail.
*
* @since 5.1.0
*
* @param bool $has_thumbnail true if the post has a post thumbnail, otherwise false.
* @param int|WP_Post|null $post Post ID or WP_Post object. Default is global `$post`.
* @param int|false $thumbnail_id Post thumbnail ID or false if the post does not exist.
*/
return (bool) apply_filters( 'has_post_thumbnail', $has_thumbnail, $post, $thumbnail_id );
}Cвязанные функции
Из метки: thumbnail (image картинки-миниатюры)
- add_image_size()
- delete_post_thumbnail()
- get_intermediate_image_sizes()
- get_post_thumbnail_id()
- get_the_post_thumbnail()
- get_the_post_thumbnail_caption()
- get_the_post_thumbnail_url()
Еще из метки: Условные теги (все)
- cat_is_ancestor_of()
- comments_open()
- email_exists()
- has_block()
- has_category()
- has_custom_header()
- has_excerpt()
- has_nav_menu()
- has_shortcode()
- has_tag()
- has_term()
- have_comments()
- have_posts()
- in_category()
- in_the_loop()
- is_404()
- is_active_sidebar()
- is_admin()
- is_admin_bar_showing()
- is_archive()
- is_attachment()
- is_author()
- is_blog_admin()
- is_blog_installed()
- is_category()
- is_child_theme()
- is_comment_feed()
- is_customize_preview()
- is_date()
- is_day()
- is_dynamic_sidebar()
- is_embed()
- is_feed()
- is_front_page()
- is_header_video_active()
- is_home()
- is_local_attachment()
- is_main_query()
- is_month()
- is_multi_author()
- is_multisite()
- is_nav_menu()
- is_network_admin()
- is_new_day()
- is_page()
- is_page_template()
- is_paged()
- is_plugin_active()
- is_post_type_archive()
- is_post_type_hierarchical()
- is_preview()
- is_robots()
- is_search()
- is_single()
- is_singular()
- is_ssl()
- is_sticky()
- is_tag()
- is_tax()
- is_taxonomy_hierarchical()
- is_textdomain_loaded()
- is_time()
- is_trackback()
- is_user_admin()
- is_user_logged_in()
- is_year()
- pings_open()
- post_exists()
- post_password_required()
- shortcode_exists()
- taxonomy_exists()
- term_exists()
- term_is_ancestor_of()
- wp_attachment_is()
- wp_attachment_is_image()
- wp_doing_ajax()
- wp_doing_cron()
- wp_is_mobile()
- wp_is_post_autosave()
- wp_is_post_revision()
- wp_script_is()