个性化阅读
专注于IT技术分析

WordPress查询3精选图片发布

点击下载

我想查询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; 

#1


Wordpres发布缩略图可用于发布元。缩略图的元密钥是_thumbnail_id。你可以使用此meta键创建查询。更多详细信息:Wordpress元查询

或仅包含具有缩略图(_thumbnail_id meta_key)的帖子, 你可以使用此查询:

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

#2


你可以使用以下功能。我测试并确认它对我有用。

$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; 
赞(0)
未经允许不得转载:srcmini » WordPress查询3精选图片发布

评论 抢沙发

评论前必须登录!