获取最近四个类别的近期帖子

时间:2012-09-02 20:03:41

标签: php wordpress

我是wordpress和php的新手,但我需要从最近的类别中获取最近的帖子。我尝试将最近帖子的类别添加到具有四个类别元素的数组中。然后列出这些类别的最后两篇文章。为此,我需要一些帮助。谢谢 !

1 个答案:

答案 0 :(得分:1)

首先,您希望使用类似this的内容来获取类别。

然后你想要获取这些类别并制作一个为those categories过滤的新WP_Query(可能使用category__in

类似

$cat_array = array();
$args=array(
  'post_type' => 'post',
  'post_status' => 'publish',
  'posts_per_page' => 5
  );
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
  while ($my_query->have_posts()) : $my_query->the_post();
    $cat_args=array('orderby' => 'none');
    $cats = wp_get_post_terms( $post->ID , 'category', $cat_args);
    foreach($cats as $cat) {
      $cat_array[$cat->term_id] = $cat->term_id;
    }
  endwhile;
}

wp_reset_query();

$args = array(
    'category__in' => $cats,
);
$query_cats = new WP_Query($args);
if ($query_cats->have_posts() ) {
    while ($query_cats->have_posts()) : $query_cats->the_post();
        //display your posts
    endwhile;
}

请注意,上面的代码尚未经过测试,但应该非常接近。

相关问题