Category Pages In WordPress

131. Category Pages in WordPress.

When a viewers clicks on a link to one of the Categories on your site, they are taken to a page listing the Posts in that particular Category in chronological order, starting from newest Posts at the top to oldest at the bottom. There are many display choices, including whether to display the complete post or post excerpts, and what additional information to display. Each theme makes different choices, and you may need to change them.

The first step in modifying what happens when someone visits a Category page is to figure out which of your theme’s files you’re going to use to display the posts. This is called the Template Hierarchy.

In the case of categories, the hierarchy is very simple. For example, suppose the slug of the Category in question is news and the Category ID is 6. The Template Hierarchy specifies that WordPress will use the first Template file it finds in your current Theme’s directory from the following list:

  1. category-slug.php
  2. category-ID.php
  3. category.php
  4. archive.php
  5. index.php

That is, if you do not have a category-slug.php, WordPress will check for a category-ID.php, and so on.

If you want to make the Category whose ID number is 6 look different from what it is currently, you may want to create a category-6.php file. In case you want to make all Category pages look different from other archive pages, then you would want to create or modify the category.php file. To make changes to the look of all archive pages, you can create or modify the archive.php file. If you modify theindex.php file, you will affect your entire blog.

If you need to create a new file, you should copy it from the next file in the hierarchy that exists. For example, if you want a special display for Category 6, start copying the category.php file, or if you don’t have one, use archive.php, and so on.

If you want some static text displayed before the list of Posts on your Category page(s). Static means text that remains the same, no matter which posts will be displayed below, and no matter which category is being displayed. Here is how to do it: above The Loop section of your Template file, insert this code:

<p> This is some text that will display at the top of the Category page. </p>

A little more complex possibility is that you want different text to display depending on which category page the visitor is viewing. Then you should add the “default” text to the main category.php file, and create special category-#.php files for each category that needs special text at the top.

This however creates a lot of files in your theme directory, and may be avoided using the following code OUTSIDE the loop:

<?php if (is_category(‘Category A’)) : ?> <p>This is the text to describe category A</p> <?php elseif (is_category(‘Category B’)) : ?> <p>This is the text to describe category B</p> <?php else : ?> <p>This is some generic text to describe all other category pages, I could be left blank</p> <?php endif; ?>

This checks to see whether you are looking at Category A, if you are then show the first bit of text, but if we’re not then check if you are looking at Category B. If you are, then show that bit of text, and if it is neither Category A or B, then show this default text.

If your Category contains more posts than will fit on one page, the category archive will split into multiple pages. And if you only want to display your static text if the viewer is on the first page of results, or you want to display different text for the other pages.

To make this happen, you may use a PHP if statement, that looks at the value of the $paged WordPress variable. It may sound complicated, but it’s actually not too bad. You just need to put the following above The Loop:

<?php if ( $paged < 2 ) : ?> <p>Text for first page of Category archive.</p> <?php else : ?> <p>Text for subsequent pages of Category. Can be left out.</p> <?php endif; ?>

Another case is to put the category name at the top of the page. If this is not already part of your template, you can add it by doing something like this, above The Loop:

<p>Category: <?php single_cat_title(); ?></p>

If you are looking to cut down on the size of your Category pages you cando this by displaying excerpts rather than the entire content of each Post. You just need to find where it says the_content() inside The Loop in your Template, and replace it with the_excerpt(). These will most likely be inside PHP tags:

<?php the_excerpt(); ?> and <?php the_content(); ?>

You can also display images linking to full posts. 

Here’s how to do that, for one post:

  1. Upload an image on the post editing screen.
  2. Switch to the “Code” editor, rather than the “Visual” editor.
  3. Use the Uploads / Browse tab to insert the image into your post. Make sure to insert the full-sized image, with no link.
  4. Copy the inserted HTML img tag, and paste it into the “Optional Excerpt” section of the post editing screen.
  5. Finish writing the post content (you can remove the image), and publish the post.

This much, guys! Hope you liked his article!

Share this!

Leave a Comment

Your email address will not be published. Required fields are marked *