But for some reason my posts get overridden instead of displaying new
data.
The problem is here: $announcements->query('showposts=1&cat=$category_id');
You are trying to use a variable inside a single quote string. Variables do not expand inside single quotes so instead of asking for cat=1
, or cat=2
, you are literally asking for cat=$category_id
— just like that, spelled out in full.
What you need instead are double quotes: $announcements->query("showposts=1&cat=$category_id");
. Variables do expand in double quote.
Or better, an array– far more readable and maintainable:
$args = array(
'posts_per_page' => 1,
'cat' => $category_id,
);
Note: showposts
is deprecated. Use posts_per_page
.