If it doesn’t work, chances are you do not have at least one post linked to that specific category 4
.
If you wanted to sort by DESC
and by date
, you wouldn’t have to specify those as arguments in the query because those are both the defaults for sorting.
Also, instead of initializing a WP_Query
object and simply using the query method, try to get in the habit of passing an array of arguments to the constructor.
So in your case, try the following:
$args = array('cat' => 4, 'posts_per_page' => 1);
$announcements = new WP_Query($args);
if ($announcements->have_posts() ) : while( $announcements->have_posts() ) : $announcements->the_post(); ?>
<h1><?php the_title() ?></h1>
<div class="post-content"><?php the_content() ?></div>
<?php endwhile; endif; ?>
Note, I did two things:
- Created an
$args
array and passed it to theWP_Query
constructor. - Check whether or not it returned any posts by doing
if ($announcements->have_posts())
before the while loop.
Take a look at this for lots of information on the WP_Query
class http://wp.smashingmagazine.com/2013/01/14/using-wp_query-wordpress/