Last updated on September 25, 2023

Add a Custom Template for a Specific Category

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Customize category layouts.

To add a custom template for a specific category in WordPress, you can utilize the category_template filter. This allows you to specify a custom template file to be used when displaying posts from a specific category.

Here’s an example code snippet that demonstrates how to add a custom template for a specific category:

function wpsnippets_custom_category_template($template) {
    if (is_category('your-category-slug')) {
        $new_template = locate_template(array('custom-category-template.php'));
        if (!empty($new_template)) {
            return $new_template;
        }
    }
    return $template;
}
add_filter('category_template', 'wpsnippets_custom_category_template');

In the code snippet above, replace 'your-category-slug' with the slug of the category for which you want to add a custom template. For example, if the category slug is “news”, you would replace 'your-category-slug' with 'news'.

Next, create a new template file named custom-category-template.php in your theme directory. This template file will be used to display posts from the specified category.

Make sure to follow the WordPress template hierarchy and include the necessary code to display the category posts in your custom template file.

By using this code snippet, you can easily create a custom template for a specific category in WordPress. This can be useful when you want to have a unique layout or design for posts within a particular category.

Examples

Example 1: Adding a custom template for a specific category

This use case demonstrates how to add a custom template for a specific category in WordPress. By creating a custom template, you can have different layouts or styles for specific categories on your website.

<?php
/**
 * Template Name: Custom Category Template
 */

// Custom code for the specific category template

In this example, we create a custom template by adding a PHP comment block at the beginning of the file. Inside the comment block, we specify the template name using the Template Name header. You can replace “Custom Category Template” with your desired template name. After the comment block, you can add your custom code specific to the category template.

Example 2: Adding a custom template for a specific category using a template file

This use case demonstrates how to add a custom template for a specific category using a separate template file. By using a separate template file, you can keep your code organized and easily manage different category templates.

Create a new PHP file in your theme directory, e.g., category-custom.php, and add the following code:

<?php
/**
 * Template Name: Custom Category Template
 */

// Custom code for the specific category template

In this example, we create a new PHP file named category-custom.php and add the same code as in Example 1. The Template Name header is still required to define the template name. After creating the file, you can assign this template to a specific category in the WordPress admin dashboard.

Example 3: Adding a custom template for a specific category using a template hierarchy

This use case demonstrates how to add a custom template for a specific category using the WordPress template hierarchy. The template hierarchy allows you to create templates based on various conditions, including category-specific templates.

Create a new PHP file in your theme directory, e.g., category-{$category_slug}.php, and add the following code:

<?php
/**
 * Template Name: Custom Category Template
 */

// Custom code for the specific category template

In this example, we use the template hierarchy to create a category-specific template. Replace {$category_slug} with the slug of the specific category you want to target. For example, if the category slug is “news”, the file should be named category-news.php. The Template Name header is still required to define the template name. After creating the file, WordPress will automatically use this template for the specified category.

Last updated on September 25, 2023. Originally posted on October 7, 2023.

Leave a Reply

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