Wordpress查询3特色图片发布

时间:2017-12-27 09:36:01

标签: php wordpress wordpress-theming

我想查询3个精选图片帖子。如果帖子没有特色图片,那么它没有显示。如果帖子有特色图片,则显示3个精选帖子。我怎么能这样做?

global $wp_query;
global $paged;
$temp       = $wp_query; 
$wp_query   = null; 
$wp_query   = new WP_Query(); 
$wp_query->query('showposts=3&post_type=post&orderby=menu_order&order=ASC'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();

the_post_thumbnail();
the_title();

endwhile; 

2 个答案:

答案 0 :(得分:0)

您可以使用以下功能。我测试并确认它为我工作。

$recent_query = new WP_Query(
                             array(
                                    'post_type'      => 'post', 
                                    'orderby'        => 'date',    
                                    'order'          => 'DESC',
                                    'post_status'    => 'publish', 
                                    'posts_per_page' => 3,
                                    'meta_query'     => array(
                                                              array( 
                                                                     'key' => '_thumbnail_id'
                                                                   ), //Show only posts with featured images
                                                               )
                                   )

                            ); 


if ( $recent_query->have_posts() ) :
    while ( $recent_query->have_posts() ) : $recent_query->the_post(); 

        if ( has_post_thumbnail() ) {
            the_post_thumbnail();
        }

        the_title();

    endwhile;

endif; 

答案 1 :(得分:0)

Wordpres帖子缩略图与post metas一起使用。缩略图元键是_thumbnail_id。您可以使用此元键创建查询。更多详情:Wordpress Meta Query

或者只包含您可以使用此查询的缩略图(_thumbnail_id meta_key)帖子:

$args = array(
  'meta_key' => '_thumbnail_id',
  'posts_per_page' => 3
);
$posts = new WP_Query( $args );
相关问题