PHPMatters Help You Better Hosting Your PHP-based Sites
How to Create WordPress Category Template Using PHP Coding

How to Create WordPress Category Template Using PHP Coding

Using the specific design and layout for each category has become a common method adopted by webmasters to make their site unique and eye-catching. This is because WordPress has a powerful template system that allows users to use the different template for different section of the whole site. In this case, you can create some customized templates for your categories coming with the particular features, such as showcasing the post excerpts, adding category images, displaying the publish dates of the posts, placing the same category introduction, adding the static text above posts, etc. The choices are unlimited, and you can customize each of your categories freely.

Before telling you how to create WordPress category template, we’d like to tell you something about WordPress template hierarchy.

WordPress Template Hierarchy

HierarchyWordPress refers to a category template based on the default hierarchical order: category-slug.php < category-id.php < category.php < archive.php < index.php.

For instance, if the slug of the category in question is “product”, then category-product.php will be used for the Product category. If WordPress fails to find the slug template, it will look for the category-id.php file. In the case that both of the two files are unavailable, it will check for the generic category.php file, and so on.

Create Category Template

To make this explanation clear and easy-to-understand, we have made an example assuming your website has a category called Product, and you want to create a unique design for this category that is different from others.

Firstly, you need to copy the content of your default category.php file. This file can be found in the folder of wp-content/themes/your current theme name. Also, you can check the file content from your WordPress admin panel. After logging to the dashboard, you can go to Appearance < Editor, and click category.php from the list of theme files appeared on your right hand. theme files

Then, you need to copy all the contents included in category.php file and paste them into a new file using a text editor. Don’t forget to save this new file with the name of category-product.php. There is a special note that the name of this file must start with the word “category”, and the descriptive word behind the hyphen symbol needs to be the same as the category name.

The next step is to create a new category template by connecting this file to your website using your favorite FTP client. Note that the file needs to be located at wp-content/themes/your current theme name of the theme directory. After that, any changes you make to this template will only appear on the product category. In the same way, you can also customize some other of your categories as you wish.

Remember that at the very top of the file, before any coding stuff, you need to put the appropriate name for your template like the example showed in the following.

category template code

Customize the Category Template

Now, it’s time to make some customizations. You only need to edit this new template file using PHP, and save the changes. We have listed some common customizing examples in below, with which you can gain some design aspirations.

Add the same introductive text above posts

add the same introductive text above posts code

Add different texts on different category

add different texts on different category

Put category name at the top of the page

put category name at the top of the page

Display the post excerpts or the full posts

display the post excerpts code


display the full post code

You’d better place one of them after the code of

display the post excerpts or the full posts code

Replace the post content with a linked image

replace the post content with a linked image

Decide which category can be seen on any page

decide which category can be seen on any page