Category Archive… A Better Way?
-
Well I just took the WP plunge a couple days ago after using MovableType for about 6 years. It is a great program (WP) but I am kind of getting in over my head with the changes in template tags / loops between the two.
I wanted to create a category archive for my site. I used a plugin for to create a date based archive (srg clean archives) but couldn’t find one for categories. I was able to get it done last night but kind of too the cheap way out and wanted to know if someone could show me in my code a better way.
First off, here is the link to the archive so you can see what I am going for:
https://abraham.dizandat.com/categories/To achieve this so far, I created a new “Page” and named it “Categories”. I put a couple sentences of text at the top and then linked it to a template page I created just for this purpose.
I was only able to (and this is like after 8 hours of trial and error) get it to spit out titles and excerpts for one category ID at a time and I couldn’t figure out how to get the category title to disply just once. There for, I have an identical chunk of code block for each category with the only change for each is “&category=x” and I hard coded the category name at the top of each block (since I couldn’t figure out how to have WP do it).
<?php
/*
Template Name: Category Archive
*/
?><?php
get_header();
?><div id="content">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="entry <?php if(is_home() && $post==$posts[0] && !is_paged()) echo ' firstpost';?>">
<h3 class="entrytitle" id="post-<?php the_ID(); ?>"> " rel="bookmark">
<?php the_title(); ?>
</h3><div class="entrybody">
<?php the_content();?>
<div id="archive-month" style="font-weight:bold">Casey and Molly</div>
<ul class="postspermonth">
<?php $posts = get_posts('numberposts=1000&orderby=post_date&order=desc&category=2'); foreach($posts as $post) : ?>- "><?php the_title(); ?> --- <?php the_excerpt(); ?>
- "><?php the_title(); ?> --- <?php the_excerpt(); ?>
- "><?php the_title(); ?> --- <?php the_excerpt(); ?>
<?php endforeach; ?>
<div id="archive-month" style="font-weight:bold">Computers</div>
<ul class="postspermonth">
<?php $posts = get_posts('numberposts=1000&orderby=post_date&order=desc&category=1'); foreach($posts as $post) : ?><?php endforeach; ?>
<div id="archive-month" style="font-weight:bold">Electronics</div>
<ul class="postspermonth">
<?php $posts = get_posts('numberposts=1000&orderby=post_date&order=desc&category=9'); foreach($posts as $post) : ?><?php endforeach; ?>
<?php edit_post_link(__('Edit')) ;?>
</div>
</div><?php endwhile; else: ?>
<?php _e('Sorry, no posts matched your criteria.'); ?>
<?php endif; ?></div>
<?php get_sidebar(); ?>
<!-- The main column ends -->
<?php get_footer(); ?>That is a shortened version (as I have 12 categories) but you get the idea. Can anyone help me do this more efficiently? You will also notice that I put a “numberposts=1000” in there so it would keep adding them. I believe the defaul value was 5 posts.
Thanks in advance.
- The topic ‘Category Archive… A Better Way?’ is closed to new replies.