WordPress.org

Ready to get started?Download WordPress

Forums

Restricting Date Archive to one category (5 posts)

  1. sonny_b
    Member
    Posted 5 years ago #

    Hi -
    Have been trying tackle this all day, and have got frustratingly close. However, not close enough.

    I'm trying to make it so that when a date archive is viewed (for example "www.site.com/2008/10"), only posts from category '5' are shown. Now, I'm aware that this can be done with permalinks and category archives (ie "www.mysite.com/work/2008/10"), however adding in date elements to the permalink would affect other sections of the site, and the client has specifically asked that they remain %category%/%postname%.

    So. I have my archive.php page set up with various Conditional tags (like if_category, if_tag etc) and using separate page templates depending on the archive type. I have added the following for the is_date function:

    elseif (is_date()) {
     	   	query_posts('cat=5');
    		include(TEMPLATEPATH . '/archive_date.php');
     	  } ?>

    When visiting a Date Archive, the correct template is being called, and all the posts are from the right category. However, the posts are not being limited by the date specified in the URL, and instead all the posts from the category are show, regardless of date posted... But here's the strange thing:
    The archive only displays posts on months when posts exist, so it is still understanding the date query. For example, I have a post in September - using "www.mysite.com/blog/2008/09" shows a list of all my posts from the Blog category, the same as it does for this month (/2008/10), which also contains posts. But if I go to a month when I made no posts (for example, /2008/05), it returns no results.

    So WordPress is understanding that I am asking for archives for dates, and understands I am asking for only posts from one category. The only problem is that it is not limiting the output of posts, and just prints all of them.

    Any help would be much appreciated,
    Thanks.

  2. sonny_b
    Member
    Posted 5 years ago #

    Anyone have any thoughts at all?
    Thanks

  3. sonny_b
    Member
    Posted 5 years ago #

    Never mind, all figured out now. Using 'query_string' seemed to do the trick. Inserted this before the Loop:

    <?php query_posts($query_string . '&cat=5'); ?>

  4. jayminkapish
    Member
    Posted 5 years ago #

    Give this a try: Date Archive for a Category

  5. Steve Taylor
    Member
    Posted 5 years ago #

Topic Closed

This topic has been closed to new replies.

About this Topic