How to Create Category Templates in WordPress

Do you want to create unique category page layouts in WordPress?

With WordPress websites, it’s common to use different templates for categories, tags, custom post types, and taxonomies.

In this article, we’ll show you how to create category templates in WordPress.

By creating templates for categories, you can add specific features on category pages.

For example, you can allow users to subscribe to categories, add category images, show category descriptions and choose a different layout for each category.

Why Create Category Templates in WordPress

WordPress generates individual pages for all your categories. You can view them by visiting a URL like:

https://example.com/category/news/

Most popular WordPress themes come with built-in templates to beautifully showcase category pages. These templates highlight the category title and show the category description below it.

However, some themes may not handle this so nicely, or you may want to customize your category pages. This is where you need to create category templates in WordPress.

Let’s take a look at how to create category templates in WordPress.

WordPress Template Hierarchy for Category Pages

WordPress has a powerful templating system that lets you create different templates for different sections of your website.

When displaying any page, WordPress looks for a template in a pre-defined hierarchical order.

To display a category page, it looks for templates in this order: category-slug.php → category-id.php → category.php → archive.php → index.php

First, WordPress will look for a template specific for that particular category using the category slug. For example, category-design.php template will be used to display the ‘Design’ category.

If it does not find a category-slug template, then WordPress will look for a template with category id, for example, category-6.php. After that, it will look for the generic category template which is usually category.php.

If there is no generic category template present, then WordPress will look for a generic archive template, such as archive.php. Lastly, it will use index.php template to display the category.

Here’s our guide to WordPress template hierarchies.

Creating a Category Template for Your Theme in WordPress

Let’s first take a look at a typical category.php template.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?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">
<h1 class="archive-title">Category: <?php single_cat_title( '', false ); ?></h1>
 
 
<?php
// Display optional category description
 if ( category_description() ) : ?>
<div class="archive-meta"><?php echo category_description(); ?></div>
<?php endif; ?>
</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_content(); ?>
 
 <p class="postmetadata"><?php
  comments_popup_link( 'No comments yet', '1 comment', '% comments', 'comments-link', 'Comments closed');
?></p>
</div>
 
<?php endwhile;
 
else: ?>
<p>Sorry, no posts matched your criteria.</p>
 
 
<?php endif; ?>
</div>
</section>
 
 
<?php get_sidebar(); ?>
<?php get_footer(); ?>

Now let’s assume that you have a category called “Design” with the category-slug “design” and you want to display this category differently than others.

To do that, you need to create a template for that particular category. Go to Appearance » Theme Editor. From the list of theme files on your right, click on category.php, if you do not have a category.php file there, then look for archive.php.

If you can’t find either of these templates, there is a good chance that you are using a WordPress Theme Framework and this tutorial may not be useful for you. We suggest that you refer to the specific framework you are using.

If you find the files above, then copy all the contents of category.php and paste them in a text editor like Notepad. Save this file as category-design.php.

Connect to your WordPress hosting using an FTP client and then go to /wp-content/themes/your-current-theme/ and upload your category-design.php file to your theme directory.

Now, any changes you make to this template will only appear in this particular category’s archive page.

Using this technique, you can create templates for as many categories as you want. Just use category-{category-slug}.php as the file name. You can find category slugs by visiting the categories section in the WordPress admin area.

Here is an example of a category-slug.php template. Notice that we have used the same template as category.php with few changes.

Since we already know the category it will be used for, we can add title, description, or any other details manually. Also notice that we have used <?php the_excerpt(); ?> instead of <?php the_content(); ?>.

Check out why we think using post summary or excerpt instead of full post is a good idea.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?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 Design category
// we can add category title and description manually.
// or even add images or change the layout
?>
 
<h1 class="archive-title">Design Articles</h1>
<div class="archive-meta">
Articles and tutorials about design and the web.
</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(); ?>

If you do not want to use the category-slug template, then you can use the category-id template to create a template for a specific category ID. Here’s how to find a category ID in WordPress.

Using Conditional Tags for a Category

When creating templates for your theme, you need to determine if you really need a separate template to do what you want to do.

In some cases, the changes you want to make are not too complicated and can be achieved using conditional tags inside a generic template, like category.php or even archive.php.

WordPress comes with support for many conditional tags that theme authors can use in their templates.

One such conditional tag is is_category(). Using this conditional tag, you can change your templates to display different output if the condition is matched.

For example, let’s suppose you have a category for featured posts called “Featured”.

Now you want to show some extra information on the category archive page for this particular category. To do that, add this code in category.php file right after <?php if ( have_posts() ) : ?>.

1
2
3
4
5
6
7
8
9
<header class="archive-header">
 
<?php if(is_category( 'Featured' )) : ?>
    <h1 class="archive-title">Featured Articles:</h1>
<?php  else: ?>
    <h1 class="archive-title">Category Archive: <?php single_cat_title(); ?> </h1>
<?php endif; ?>
 
</header>

Create a Category Template Using Beaver Themer

Beaver Themer allows you to create layouts for your theme. You can select the individual categories where you want to use the template and then edit it using a drag and drop tool.

First, go to Beaver Builder » Themer Layouts » Add New page.

You’ll need to give it a title and then select your category under ‘Location’ option.

From there, you’ll be able to use Beaver Builder’s drag and drop editor to customize your category layout page to your liking.

Once you are done, click on the Done button and then select publish to apply your category template.

You can now visit your website to see the category template in action.

We hope this article helped you learn how to create category templates in WordPress. You may also want to see our comparison of best drag & drop WordPress page builders for creating custom layouts, and our guide on how to create a membership site, so you can restrict content based on categories.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

Leave a Reply

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

error: Content is protected !!

Pin It on Pinterest

Add address