Php 从Wordpress中循环的类别的子类别中获取最新的5篇文章

Php 从Wordpress中循环的类别的子类别中获取最新的5篇文章,php,wordpress,loops,Php,Wordpress,Loops,我需要从一个类别的子类别得到最新的5篇文章。 我该怎么做 //编辑 所以我让它为每个子类别的固定数量的帖子工作: <?php $descendants = get_categories(array('child_of' => 3)); ?> <?php $cnt=1; foreach ($descendants as $child) { ?> <?php $catPosts = new WP_Query(); $catPosts -> quer

我需要从一个类别的子类别得到最新的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; 
}?>

我将使用get_categories()循环遍历所有子类别,并将它们的ID添加到数组中。然后,您可以将该数组用于新WP_查询的category_u_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(); ?>

//把你的循环代码放在这里
没有找到帖子

我认为这个问题很清楚,即使它很短。如果你真的想要帮助并且需要更多信息,请告诉我。让我展开,S.O问题应该包含代码,应该告诉我们你尝试了什么,失败的地方。。。我什么都没试过,因为我想不出解决办法。我试着在谷歌上找到我的问题的答案,但没有成功。你什么都没试过,也没有结果?我很震惊。我只知道如何从一个类别或所有类别获得帖子。我知道这一次对我不起作用,因为那不是我需要的,正是我想要的。非常感谢你!