I want to output latest post from each category (child category) that has parent. Parent category id is 54.

For example, if there are 7 child categories under the category 54, the number of output post should be 7 (all latest from each child category). I hope this makes sense.

My current code is below. At this stage, this code outputs only one latest post (of 1 child category) that has the latest under cat id=54. It would be great if you could advise me how to modify this so that I can get more latest post from multiple child categories.

<?php 
$categories = get_categories();
foreach ( $categories as $category ) {
    $args = array(
    'cat' => 54,
    'post_type' => 'post',
    'posts_per_page' => '1',
    );
}
?>
<?php $query = new WP_Query( $args ); ?>
<?php if ($query->have_posts()) : ?>
<div class="container">

<?php while ($query->have_posts()) : $query->the_post(); ?>   
<div class="box">
<article>
    <p><?php foreach((get_the_category()) as $childcat) { if (cat_is_ancestor_of(54, $childcat)) { echo '<a href="'.get_category_link($childcat->cat_ID).'">'; echo $childcat->cat_name . '</a>'; }} ?></p>
    <?php if ( has_post_thumbnail() ): ?><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('box-pic'); ?></a><?php endif; ?>
    <h3><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h3>
</article>
</div>
<?php endwhile;?>

</div>
<?php endif; ?>
<?php wp_reset_query(); ?>

Read more here: Output latest posts from each child categories of particular parent category


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: