I have a parent category which has a lot of child categories, which also have a lot of their child categories and these categories have posts. I want to show on page all the child categories titles from the first parent category and posts titles only once. Now I have code which shows posts several times after every loop iteration, but I want to display it only once. Here is my code snippet
# get child categories
$sub_cats = get_categories( array(
'child_of' => $parent_id,
'hide_empty' => 0
) );
if( $sub_cats ){
foreach( $sub_cats as $cat ){
echo '<h3>'. $cat->name .'</h3>';
# get posts from category
$myposts = get_posts( array(
'numberposts' => -1,
'category' => $cat->cat_ID,
'orderby' => 'post_date',
'order' => 'DESC',
) );
# show posts
global $post;
foreach($myposts as $post){
setup_postdata($post);
echo '<li class = "test"><a href="'. get_permalink() .'">'. get_the_title() .'</a></li>';
}
}
wp_reset_postdata();
}
I'm not WordPress developer, but I need to do this task. I didn't write this code, I just found it on the internet. Can someone help me to show posts only once or improve this code? thank you