我是wordpress和php的新手,但我需要从最近的类别中获取最近的帖子。我尝试将最近帖子的类别添加到一个包含四个类别元素的数组中。然后拿出这些类别的最后两个帖子。为此我需要一点帮助。谢谢
因此,首先您需要使用类似的东西来获取类别。
然后,您将希望获得这些类别并创建一个新的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;
}
请注意,上面的代码还没有经过测试,但应该相当接近。