Resources     Blog  

  1. Home
  2. WordPress Tutorials
  3. How to Add and Use Category Templates in WordPress

How to Add and Use Category Templates in WordPress

Do you want to use different category templates on your WordPress website? WordPress allows you to use different templates throughout your website, which can be used for different categories. In fact, this is how many custom themes are created in the first place. Sadly this feature is not easy to use and will require you to edit files on the backend of your website.

Using different templates for your categories makes each one feel unique to your visitors and shows just how creative your website is. Keep in mind that using category templates is different from styling your categories. Today, I will demonstrate how to add and use category templates in WordPress.

What Are and Why Should You Use Category Templates

A category template allows for unique features and designs to be active for a single category, while other categories may use different templates. For example, if you have a dogs category, you may have a template that focuses on dog specific items like bones, paw prints, and dogs themselves.

Using category templates can really help your website stand out from the thousands of competitors you will face. It makes each category look meaningful and helps each piece of content stand out from one another. Being unique goes a long way when your website is one in a million. Every design choice matters and needs to be carefully made and be meaningful.

How to Add and Use Category Templates in WordPress

Today, I will demonstrate how to add and use category templates in WordPress. Before you begin this tutorial, you must take the time to create categories and add content to them. Adding category templates to WordPress requires you to edit files in the backend of your website. WordPress uses a hierarchy system to determine what template to load. By default, WordPress will use the theme’s templates, but by adding custom templates, WordPress will load and use those first.

This process may be slightly different depending on the theme you are using. You should also consider creating a backup of your website before beginning. This will ensure that you can fix your website if something goes wrong.

Finding Out the Current Hierarchy

You must first determine what your theme is loading. If you have never used custom templates before most likely your theme is reading the category.php file. If this is absent then the archive.php file will contain the information, but if this is also missing then the index.php file will be used. The key is to add a file that will be read before these.

For reference, here is what the template hierarchy system looks like in WordPress.

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

The files are read from 1 to 5 and if a file is not found it will move down.

Let’s start by logging into the cPanel and clicking on the File Manager option. The File Manager will allow you to access all of the files related to your website.

Click on the File Manager option.

You need to locate your current theme’s folder because the files you need will be located within it. Click on the public_html directory, then click on the wp-content folder. Inside of this folder, you will find all of the content related to your website. Click on the themes folder and enter the folder of the theme you are currently using.

Inside of this folder, you will see important files like the functions.php file, which is one of the most edited files in WordPress. Look through all of the folders and look for the categories.php, archives.php, and index.php files. In my case, I only had the archives.php and index.php files.Since archives.php is currently the highest ranked file, according to the hierarchy system, it will be read first, so I must introduce a file above that.

Adding a Category Template

Now that you know the current hierarchy you are ready to add a template. Alternatively, you could just add a category-slug.php file since it is always the first in the hierarchy. Make sure you name the file appropriately, for example, a category of “Dogs” with a slug of “dogs”, means that it will be named category-dogs.php. Make sure to confirm that your slug name is correct, or set one up if you have not already.

We need to add a new file by clicking on the “+ File” button.

Click on the "+ File" button

A pop-up window will appear. Name the file appropriately and click on the “Create New File” button. If you do not name it correctly or misspell something, it will not work.

Click on the "Create New File" button.

Your file in now created, but it is empty. Here is an example of a category-slug template file. This is only an example and must be edited to work correctly for your website: 

<?php
/**
* A Simple Category Template
*/

get_header(); ?>

<section id=”primary” class=”site-content”>
<div id=”content” role=”main”>
<?php
// Check if there are any posts to display
if ( have_posts() ) : ?>

<header class=”archive-header”>
<?php
// Since this template will only be used for your selected category
// we can add it’s category title and description manually.
// or even add images or change the layout
?>

<h1 class=”archive-title”>Dogs Articles</h1>
<div class=”archive-meta”>
Content about dogs for dog enthusists.
</div>
</header>

<?php

// The Loop
while ( have_posts() ) : the_post();
<h2><a href=”<?php the_permalink() ?>” rel=”bookmark” title=”Permanent Link to <?php the_title_attribute(); ?>”><?php the_title(); ?></a></h2>
<small><?php the_time(‘F jS, Y’) ?> by <?php the_author_posts_link() ?></small>

<div class=”entry”>
<?php the_excerpt(); ?>

<p class=”postmetadata”><?php
comments_popup_link( ‘No comments yet’, ‘1 comment’, ‘% comments’, ‘comments-link’, ‘Comments closed’);
?></p>
</div>

<?php endwhile; // End Loop

else: ?>
<p>Sorry, no posts matched your criteria.</p>
<?php endif; ?>
</div>
</section>

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

Once you have set up your category template file, click on the “Save Changes” button to finish.

Click on the "Save Changes" button.

Congratulations, your website will now load the category template whenever a piece of content is under your specified tag. You can add multiple templates, but make sure they are unique enough to make it worth your time.

Fully Customize Your Website

Customizing your website is very important and helps your website stand out from the competition. Customization can range from category templates all the way down to the color of your theme. How your website looks is almost as important as how your website runs. Many visitors will not try a website if it does not look good.

How many category templates have you created? How unique are your templates from each other?

Author: Ron Helms

I currently work for GreenGeeks as a Support Technician. My primary roles are supporting our VPS and Dedicated server clients, as well as performing site migrations. With experience in the web hosting industry since 2009, there is rarely a question I can’t help answer. In my spare time, I enjoy gaming and working on cars as an automotive enthusiast.

Was this article helpful?

Related Articles

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.