I'm displaying the 3 most recent posts from a parent category ("Where We Serve") on a page. Within that parent category, I have 6 other categories named by regions ("Africa", "Europe", "Asia", etc.). The page displays the region category name, and the post content below it. Here's the catch; of these 3 most recent posts, sometimes there will be 2 from the same region category. When that happens, I need the page to show the region category for ONLY the first post from that category. Hopefully this code explains what I'm trying to do:
<div class="news">
<?php
$args = array( 'numberposts' => '3', 'category' => 9 );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
$category = get_the_category($recent["ID"]);
if(
$category == //any previous post's category on this page
){
//echo the post WITHOUT the category name displayed
echo '<h2>'.$recent["post_title"].'</h2><br>'.$recent["post_content"].'<br>';
}
else{
//echo the post WITH the category name displayed
echo '<h1>'.$category[0]->cat_name.'</h1><br><h2>'.$recent["post_title"].'</h2><br>'.$recent["post_content"].'<br>';
}
}
?>
</div>
I don't know how to test for other posts' categories on that page.