How to create a template for a specific category in WordPress

WordPress

The best thing about WordPress is its flexibility. You can customize it the way you want. Recently, I was making some changes to one of my sites. I realized that I need to change the design of only one category page to make it stand out. If your theme doesn’t allow the customization of category pages, you can manually do this easily.

WordPress has an advanced and easy template system that lets you create templates for different sections of your WordPress website. If I only take a look at the category page, you can customize the category pages or just have a custom look for an individual category. The order of category page templates is: category-slug.php -> category-id.php -> category.php -> archive.php -> index.php.

Best Managed WordPress Hosting

WPEngine is the best and most secure managed hosting provider

WordPress looks for category page templates in the given order. If you want to make a common category page for all the categories, you can choose to make a category.php. For changing the category page for a specific category, use category-slug.php or category-id.php file.

Now let’s assume you have a category called “Plugins” with slug ‘plugins’ and id 5 on your blog. When you open the plugins category page, WordPress will first look for the category-plugins.php file. If this file is not there, WordPress will look for category-5.php. If this file also doesn’t exist, WordPress will try for category.php. If none of the generic category templates is present, it will try to find archive.php. If none of these work, it will use the index.php file to display that category page.

If you want to make a custom page template for the plugins (slug: plugins) category of your blog, you can create a new category-plugins.php file. Then write the code for the template using WordPress functions and tags.

Here’s a sample code for category page.

<?php
/*
Template Name: Category Plugins Template
*/
get_header();
?>
<section id="primary" class="main-content">
<div id="content" role="main">
<?php 

if ( have_posts() ) : ?>
<header class="header">
<h1 class="title">Category: <?php single_cat_title( '', false ); ?></h1>
 
<?php
if ( category_description() ) : ?>
<div class="meta"><?php echo category_description(); ?></div>
<?php endif; ?>
</header>
 
<?php
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(); ?>

If you already have a category.php file, copy the same code in this new single category page template. It will have the same HTML structure. Later you can customize it the way you want.

Now you know how to create category templates in WordPress. You can easily make a new file for that category and customise is the way you want.

I hope this article was useful. You can explore more articles on TheWPGuides to learn WordPress and WordPress optimisation.

Tags: |

Deepanker Verma is an experienced WordPress developer who has been working on WordPress for more than 12 years. On TheWPGuides, he writes about WordPress, WordPress development, and WordPress plugins.


Similar Articles

0 Comments

Leave a comment

Comment policy: We love comments and appreciate the time that readers spend to share ideas and give feedback. However, all comments are manually moderated and those deemed to be spam or solely promotional will be deleted.

© 2022 The WP Guides Developed By Deepanker