Building the Category Template Page
Sunday, October 11th, 2009 by MikeLTFor the category page, I wanted to place a heading to let you know which category you were looking at, and maybe a short description of what that category is about. I place these tags just after the content div before the loop starts.
I also edited the category out of the post meta data to avoid the redundancy.
Since this website is still pretty young, I have some categories with more posts than others, in fact, some new categories will actually start out with only one post, as this is the nature of growth.
Instead of having one page with only a title and a couple of lines of text, I will first use a php query to determine if there a more than two posts in the category, and then run two while loops.
If the query returns true, that there are more than two posts, I will show only the exerpts. Otherwise, (else), the while loop runs again showing the full post.
The only thing is that if I use the more tag for the home page, that will also show up here (as well as the other template pages), truncating the post anyway. I gotta think there’s a workaround for that, but for now I just got to remember to lose the more tag as the post leaves the home page.
<p>
<h3> <?php single_cat_title(‘You are browsing the category: ‘); ?></h3>
<?php echo category_description(); ?>
</p>
<!–WP-Loop for category.php–>
<?php if (have_posts()) : ?>
<?php if (($wp_query->post_count) > 2) : ?>
<?php while (have_posts()) : (the_post()); ?>
<h2 id=”post-<?php the_ID(); ?>”>
<a href=”<?php the_permalink() ; ?>” rel=”bookmark” title=”Permanent link to <?php the_title() ; ?>”> <?php the_title(); ?></a></h2>
<small>Posted on <?php the_date(); ?> at <?php the_time(); ?> by <?php the_author(‘,’); ?></small>
<?php the_excerpt(); ?>
<p>
<?php edit_post_link(‘Edit’,”,’<strong>|</strong>’); ?>
<?php comments_popup_link(‘No Comments »’, ‘1 Comment »’, ‘% Comments »’); ?></p>
<?php endwhile; ?>
<?php else : ?>
<?php while (have_posts()) : (the_post()); ?>
<h2 id=”post-<?php the_ID(); ?>”>
<a href=”<?php the_permalink() ; ?>” rel=”bookmark” title=”Permanent link to <?php the_title() ; ?>”>
<?php the_title(); ?></a></h2>
<small>Posted on <?php the_date(); ?> at <?php the_time(); ?> by <?php the_author(‘,’); ?></small>
<?php the_content(); ?>
<p>
<?php edit_post_link(‘Edit’,”,’<strong>|</strong>’); ?>
<?php comments_popup_link(‘No Comments »’, ‘1 Comment »’, ‘% Comments »’); ?></p>
<?php endwhile; ?>
<?php endif; ?>/*ends the php query IF > 2*/
<?php else : ?>
<h2>Not Found</h2>
<p>Sorry, but you are looking for something that isn’t here.</p>
<?php get_search_form(); ?>
<?php endif; ?>
Tags: category page, hierarchy, template page, the loop
Leave a Reply
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.



