Herself's Webtools

Scripts, HowTos, Templates, Plugins, Widgets, Tips

Archive for the ‘archives’ Category

How to create a list of posts by category in WordPress

with 2 comments

I have to say creating a link lists of posts by category in WordPress is downright painful. To create the archive list ( see link near search box on sidebar ) you need a list of your categories, the category number. You will use these to create an archives.php page.

1) A list of your categories can be found by going to the Dashboard, then drop down the Posts menu and select categories.  These are the headings you put between the <br><h3>Category Name</h3> in the php below.

2) Category numbers had to be dug out of my database.  I used MySQL Admin, went to my WordPress database.  Select your database, find the wp_terms table and click on the leftmost icon under ‘Action’ to list your tags and categories. The link_id is the category number.

3) PHP code copy and paste this to a file named Archives.php.  Change the category names and numbers to match those on your website.  Also add or delete as many categories as you need. Your template is likely different than mine you may need to adjust the sidebar, footer etc to match your template’s style

4) showposts=200 is the maximum number of posts it will show in that category, adjust if you need to.

<?php
/*
Template Name: Archives
*/
?>

<?php get_header(); ?>

<div id=”content”>

<h2>Archives</h2>

<div>

<p>Complete archive of the blog’s posts sorted by category</p>

<br><h3>Fun things to do in Houston</h3>
<?php query_posts(‘cat=5&showposts=200′); ?>
<?php while (have_posts()): the_post(); ?>
<li><a href=”<?php the_permalink() ?>”><?php the_title(); ?></a></li>
<?php endwhile; ?>

<br><h3>OMG We’re in Texas</h3>
<?php query_posts(‘cat=7&showposts=200′); ?>
<?php while (have_posts()): the_post(); ?>
<li><a href=”<?php the_permalink() ?>”><?php the_title(); ?></a></li>
<?php endwhile; ?>

<br><h3>The Move</h3>
<?php query_posts(‘cat=15&showposts=200′); ?>
<?php while (have_posts()): the_post(); ?>
<li><a href=”<?php the_permalink() ?>”><?php the_title(); ?></a></li>
<?php endwhile; ?>

<br><h3>This ‘n That</h3>
<?php query_posts(‘cat=14&showposts=200′); ?>
<?php while (have_posts()): the_post(); ?>
<li><a href=”<?php the_permalink() ?>”><?php the_title(); ?></a></li>
<?php endwhile; ?>

<br><h3>Tokyo Trip</h3>
<?php query_posts(‘cat=172&showposts=200′); ?>
<?php while (have_posts()): the_post(); ?>
<li><a href=”<?php the_permalink() ?>”><?php the_title(); ?></a></li>
<?php endwhile; ?>

<br><h3>Geekiness</h3>
<?php query_posts(‘cat=83&showposts=200′); ?>
<?php while (have_posts()): the_post(); ?>
<li><a href=”<?php the_permalink() ?>”><?php the_title(); ?></a></li>
<?php endwhile; ?>

<br><h3>Photos</h3>
<?php query_posts(‘cat=10&showposts=200′); ?>
<?php while (have_posts()): the_post(); ?>
<li><a href=”<?php the_permalink() ?>”><?php the_title(); ?></a></li>
<?php endwhile; ?>

</div>
</div>

<?php get_sidebar(); ?>
<?php get_footer(); ?>

5) Upload your archives.php file to your theme directory on your webserver.

6) Finally create a new page – drop down the attributes menu on the right and select archives as your template

Or if you’d rather there’s a new plugin out to do display all posts in all categories for you James Wilkes Web Design

Written by Linda MacPhee-Cobb

April 2nd, 2010 at 11:51 am

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.

4/2/10 See also How to lists posts by category in WordPress; for a newer, easier way to do this.

Written by Linda MacPhee-Cobb

February 25th, 2008 at 5:00 am