WP_Query 获取带有特色图片的文章的方法

在 WordPress 的 WP_Query 函数中,我们可以通过这时 meta_query 参数来筛选查询结果,设置 key 值为 _thumbnail_id 可以获取带有特色图片的文章。

<?php 
    $args = array( 
    'cat' => 1,
    'meta_query' => array(array('key' => '_thumbnail_id')),
    'posts_per_page' => 2,
    'post_status' => 'publish'
    ); 
    $query = new WP_Query( $args ); 
    while ( $query->have_posts() ) : $query->the_post();
?> 
        <div class="swiper-slide">
            <a href="<?php the_permalink(); ?>">
                <?php the_post_thumbnail(); ?>
            </a>
        </div>
<?php endwhile; wp_reset_query();?>