Herself's Webtools

Scripts, HowTos, Templates, Plugins, Widgets, Tips and Useful Information

Create a separate archives page for each category in your Wordpress blog

with 12 comments

I received a request for this on the ArchivesByCategory plugin page. Unfortunately there isn’t a plugin I can write that will paginate the archives one category to a page for you. But it is not too difficult to do with this plugin.

The plugin takes one category number from you and creates a link list of all the posts in that category.

You must create a separate archive page for each category. Since every template is different it is hard to give really detailed directions but I’ll try.In your theme find your archives.php page. Make a copy one for each category. archives1.php, archives2.php etc.

In archives1.php change

Template Name:ArchivestoTemplate Name: Archives 1change<h2>Archives</h2>to<h2>Category 1’s name</h2>Change<?php wp_list_categories(); ?>to<?php echo archive_of_posts(1); ?>

Now you have to do this for each archive page you want, one per category. The above example is for your first category.  For your next category replace the 1s with 2s.Lastly go to your wp-admin->write->page section. Then go to the Page Template drop down menu. You will see an entry for each archive page you made. Create one new page for each template archive entry. Give the page a title ‘Archives for Category 1′ or what ever you wish to call it. Nothing else. The template will fill in the page for you.

Both the plugin and the default WP theme that have been altered are included in the file below. That way you can see what I have done. Do not upload the default theme to your WP site. Just upload the plugin.

The theme is just to make it easier for you to see what I have done.Archives example and plugin

I’ve done this on two of my blogs so far, Herself’s Houston Garden, and Herself’s House Plants. It is much cleaner and a much nicer way of sorting your archives. I hope to have all my blogs updated to this method in the next week or two.

Written by timestocome

February 25th, 2008 at 5:00 am

12 Responses to 'Create a separate archives page for each category in your Wordpress blog'

Subscribe to comments with RSS or TrackBack to 'Create a separate archives page for each category in your Wordpress blog'.

  1. Wondering if the below is possible? Your solution is so close to what i’ve been trying to implement…

    To have a single hwt_archive.php with a corresponding Page which has multiple links e.g. something.com?page=100&cat1=7&cat2=11. When a user clicks the link it opens the page (ID 100) which is based on the template i.e. hwt_archive.php. The .php file is able to parse the URL and lists posts of categories 7 *AND* 11 i.e. using a mutually inclusive query_post statement: query_posts(array(‘category__and’ => array(7,11)));

    A list of posts (and/or post titles) are displayed as a result of executing the query_posts statement through parsing the URL parameter variables.

    Is this possible? Happy to contract this also!

    aidee

    30 Oct 08 at 8:08 am

  2. Yes you can do that.

    No I don’t do contract work. What is here is stuff I needed for myself that I thought others would find useful as well.

    I would just use two separate mysql commands one for each of the two categories you wish to build a list of links.

    Duplicate the $last_posts section and foreach that builds the links before return $the_output.

    You’ll have to experiment a bit to get it to look nice but that should work.

    ljmacphee

    30 Oct 08 at 9:58 am

  3. Thanks Linda!

    I’ve tried the following which parses (and echo’s) the URL and its components

    $url = $_SERVER['REQUEST_URI'];
    parse_str($url, $query);
    var_dump($query);

    $cat1 = (int)$query[ 'cat1' ];
    $cat2 = (int)$query[ 'cat2' ];

    echo ‘cat1 var dump:’ . var_dump($cat1);
    echo ‘cat2 var dump:’ . var_dump($cat2);
    echo ‘cat1:’ . $cat1;
    echo ‘cat2:’ . $cat2;

    query_posts(array(‘category__and’ => array($cat1,$cat2),’orderby’=>title,’order’=>ASC));

    But the last line i.e. the query_posts() just does not want to play… All of the above is sitting the template.php file.

    aidee

    31 Oct 08 at 8:12 pm

  4. I think you want ‘order ASC’ and order by ‘title’

    ljmacphee

    31 Oct 08 at 8:44 pm

  5. I thin i’ve solved my dilemma, i’ve used a default template and executed all the php within the Page:

    array((int)$query[ 'cat1' ],(int)$query[ 'cat2' ]),’orderby’=>title,’order’=>ASC)); ?>

    I can now form a URL as such:
    http://grieflink.org.au/?page_id=360&cat1=9&cat2=13

    aidee

    31 Oct 08 at 9:23 pm

  6. Cool, very nice.

    ljmacphee

    31 Oct 08 at 9:37 pm

  7. Whoops stripped out the php:

    $url = $_SERVER['REQUEST_URI'];
    parse_str($url, $query);
    query_posts(array(‘category__and’ => array((int)$query[ 'cat1' ],(int)$query[ 'cat2' ]),’orderby’=>title,’order’=>ASC));

    aidee

    31 Oct 08 at 11:04 pm

  8. Thank you for that code.

    When I have trouble finding code on line I find it isn’t just me looking for it. Other people people are looking for the solution too.

    Someone will be grateful you published it.

    ljmacphee

    1 Nov 08 at 9:48 am

  9. And thank you for sharing this wonderful resource which is Herself’s Webtools!

    Oh and relating to the Archives of Posts plugin (included in the example download on this page), I noticed that you can make just a reference on a Page to display posts of a category (wrapped in a php tags) e.g.:

    echo archive_of_posts(187);

    No need for a template.php, although the code is derived from one of the archive.php template files!

    Too cool!

    aidee

    1 Nov 08 at 9:24 pm

  10. Thanks! I hadn’t tried the echo php on that plugin.

    I did find the echo plugin got flaky when I used it with the Coppermine plugin. So if things do get flaky start looking there. But that was the only plugin that flaked out with echo.

    ljmacphee

    3 Nov 08 at 9:06 am

  11. Hello again!

    Just out of curiosity, have you ever come across the problem where a Page has a bit of PHP at the top and then normal Page text under the PHP call but which displays in the reverse order?

    I have a simple Page which has a query_posts() bit of PHP with unassuming text underneath. However when viewed in a browser the text appears first on the page and then the result of the php code. Normally it displays fine but then somehow starts displaying in this reverse order and I for the life of me cannot explain why – I usually delete the Page and create a new one…

    Cheers!

    aidee

    24 Nov 08 at 7:40 am

  12. I’ve seen that when table tags are not correct. Are you using tables?

    Also if the php start and end tags aren’t in their proper spots.

    ljmacphee

    24 Nov 08 at 8:50 am

Leave a Reply

You must be logged in to post a comment.