I have a basic archives.php file. I was trying to get it to display only posts from category 1. When I do query_posts(‘cat=1’), the URL is then ignored and all posts in category 1 are displayed.
For example, lets say I have one post in category 1: it’s called “testing post” and is dated November, 1, 2012. Lets say I have another post in category 5 dated April 2, 2012.
With the current archives.php (using the query posts for cat 1) the URL: http://testing.com/2012/04 resolves to show all posts within category 1. No good, since the post is dated November, and the URL is querying for April (04).
The archives.php will resolve http://testing.com/2012/04 and http://testing.com/2012/11 as each of those months have a post in them (albeit not from category 1), it will not however resolve http://testing.com/2012/09 as that month doesn’t have any posts (category 1 or otherwise)
How can I make the archives.php file query for the current month (whatever is in the URL) but only display posts in category 1? Asked another way, how can I make http://testing.com/2012/04 come up with a “sorry there are no posts here” message, like in http://testing.com/2012/09 while http://testing.com/2012/11 still displays the one post from category 1?
First: don’t use
query_posts()
, ever.Second, to create a category archive index page template for a specific category, refer to the Codex entry for the Template Hierarchy:
category-{slug}.php
category-{id}.php
category.php
archive.php
index.php
So, if you have a category,
'foobar'
, with a category ID of1
, you could do either of the following:category-foobar.php
category-1.php
And WordPress will use that template to render the archive index page for that category.
The reason your query is getting stomped has nothing to do with your template file, however; it is because you’re completely overriding the default query, by using
query_posts()
.To filter your date-based archives by a specific category, use
pre_get_posts
instead:For more information, see Nacin’s WordCamp presentation, You Don’t Know Query.