您可以使用该功能wp_list_pluck 获取从查询中检索到的帖子的ID。
只需将查询移到h2元素之前,并获取posts ID:
// Set a bool to know if at least one post has post thumbnail.
$has_thumb = false;
$first_query = new WP_Query(\'cat=1&author=\' . $post->post_author . \'&order=DESC&tag=Tattoo&posts_per_page=1000\');
// Check if the query have posts.
if ( $first_query->have_posts() ) {
// Get the posts id\'s.
$ids = wp_list_pluck($first_query->posts, \'ID\');
foreach( $ids as $id ) {
if ( has_post_thumbnail($id) ) {
$has_thumb = true;
break; // If at least we have a post thubmnail we don\'t need to continue.
}
}
}
然后,您可以检查是否有缩略图,如果有,则显示h2。
<?php if ( $has_thumb ) : ?>
<h2>Tattoo Work by <span style="text-transform: capitalize;"><?php the_author_meta(\'user_nicename\'); ?></span>:</h2>
<?php endif; ?>