WordPress 101 主题开发教程: 10 – 通过分类过滤WP_Query结果

WordPress 101 – Part 10: Filter the WP_Query with categories

视频学习参考: https://www.youtube.com/watch?v=e8nJMopiH2Q&list=PLriKzYyLb28nUFbe0Y9d-19uVkOnhYxFE&index=10

本节课的重点函数是 get_categories();

$args_cat = array( ‘include’ => ‘1, 9 ,8’);

$categories = get_categories($args_cat);

foreach($categories as $category):

$args = array(
    'type' => 'post',
    'posts_per_pages' => 1,
    'offset' => 0,
    //'category__in' => array(),
    //'category__not_in' => array(),
   'category__in' => array($category->term_id), 
);
$lastBlog = new WP_Query($args);
if ( $lastBlog -> have_posts() ) :
    while ( $lastBlog -> have_posts() ) :
        $lastBlog -> the_post();
        the_title();
    endwhile;
endif;

endforeach;

** 当一篇文章选择归属于2个分类时 会同时属于2个分类

Super happy!