我需要从类别的子类别中获取最新的5个帖子。 我该怎么做?
//编辑 所以我让它为每个子类别的固定大量帖子工作:
<?php $descendants = get_categories(array('child_of' => 3)); ?>
<?php $cnt=1;
foreach ($descendants as $child) { ?>
<?php $catPosts = new WP_Query();
$catPosts -> query("showposts=3&cat=$child->term_id");
?>
<ul class="postPreviews">
<?php while ($catPosts->have_posts()) : $catPosts->the_post(); ?>
<li><a href="<?php the_permalink() ?>">
<div class="descOverlay">
<img src="<?php the_field('teaserimage'); ?>" />
<div class="overlayTitle">
<div class="imgwrap"><img src="<?php echo get_template_directory_uri(); ?>/opaq.png"></div>
<span><?php the_title(); ?></span></div>
</div>
</a></li>
<?php endwhile;
}?>
答案 0 :(得分:4)
我会使用get_categories()遍历所有子类别并将其ID添加到数组中。然后,您可以将该数组用于新的WP_Query的category__in'参数。
<?php
$categories = get_categories( array(
'child_of'=>'your_category_id'
) );
$subcategories = array();
foreach ( $categories as $category ) {
$subcategories[] = $category->cat_ID;
}
?>
<?php
$new_loop = new WP_Query( array(
'post_type' => 'post',
'category__in' => $subcategories,
'posts_per_page' => 5
) );
?>
<?php if ( $new_loop->have_posts() ) : while ( $new_loop->have_posts() ) : $new_loop->the_post(); ?>
// put your inside the loop code here
<?php endwhile; else: ?>
No posts found
<?php endif; ?>
<?php wp_reset_query(); ?>