下面的代码将查询“blog”类别中最近发布的三篇文章,并循环这些结果以显示每个文章的摘录。
$args = array(
\'category_name\' => \'blog\',
\'post_status\' => \'publish\',
\'posts_per_page\' => 3,
);
$blog_posts = new WP_Query( $args );
if ( $blog_posts->have_posts() ):
while ( $blog_posts->have_posts() ):
$blog_posts->the_post();
// Do stuff with each post
echo get_the_excerpt() . "<br>";
endwhile;
wp_reset_postdata();
endif;
WordPress Codex是WP开发的重要资源。WP\\u Query类功能强大,在这里有很好的文档记录:
https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters