Your foreach
is closed prematurely.
foreach( $categories as $catergory ) {
// query args
$args = array(
// args
);
}
// generate the loop
$the_query = new WP_Query ( $args );
// output the loop
while ( $the_query->have_posts()) : $the_query->the_post();
// Loop markup
endwhile;
wp_reset_postdata();
You’re saying:
- Step through each of the categories; for each category, add query results to
$the_query
. - After stepping through the categories, output a loop for
$the_query
So, you’re only ever going to display the loop for the last category in $categories
.
Instead, you need to put the loop output inside your foreach
loop:
foreach( $categories as $catergory ) {
// query args
$args = array(
// args
);
// generate the loop
$the_query = new WP_Query ( $args );
// output the loop
while ( $the_query->have_posts()) : $the_query->the_post();
// Loop markup
endwhile;
wp_reset_postdata();
}
That way, you output a loop for each category, rather than just the last category.