我想查询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;
发布于 2017-12-27 18:22:25
您可以使用以下函数。我测试并确认它对我来说是有效的。
$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;
发布于 2017-12-27 18:33:20
Wordpres post缩略图与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 );
https://stackoverflow.com/questions/47989659
复制相似问题