Create single.php for specific category by category id

I'm trying to create template for specific category with id 1 for example. The template should be applied to all of the pages belongs to this category. So, it should be something like single-1.php.

I tried a lot of solution that I found on the web, including this, but it seems nothing is works for me.

Topic single categories templates customization Wordpress

Category Web


Worth mentioning that if you are using a child theme then use the following code to retrieve category single post template.

function wpse_category_single_template( $single_template ) {
    global $post;
    $all_cats = get_the_category();
    if ( $all_cats[0]->slug == 'cat1' ) {
        if ( file_exists(get_stylesheet_directory() . "/single-cat1.php") ) return get_stylesheet_directory() . "/single-cat1.php";
    }
    return $single_template;
}
add_filter( 'single_template', 'wpse_category_single_template' );

You can make use of the single_template filter, in_category() condition and the queried object to test if the current post is attached to our specified category and if so, include our desired template

add_filter( 'single_template', function ( $template )
{
    // Get the current single post
    $post_id = $GLOBALS['wp_the_query']->get_queried_object_id();

    // Test to see if our post belongs to category 1
    if ( !in_category( 1, $post_id ) ) {
        return $template;
    }

    // Our post is attached to category 1, lets look for single-1.php
    $locate_template = locate_template( 'single-1.php' );

    // Test if our template exist, if so, include it, otherwise bail
    if ( !$locate_template ) {
        return $template;
    }

    return $locate_template;
});

You can use this function to add category specific single template pages on your website. This goes in functions.php

You can define as many single templates as you want.

function wpse_category_single_template( $single_template ) {
    global $post;
    $all_cats = get_the_category();

    if ( $all_cats[0]->cat_ID == '1' ) {
        if ( file_exists(get_template_directory() . "/single-cat1.php") ) return get_template_directory() . "/single-cat1.php";
    } elseif ( $all_cats[0]->cat_ID == '2' ) {
        if ( file_exists(get_template_directory() . "/single-cat2.php") ) return get_template_directory() . "/single-cat2.php";
    }
    return $single_template;
}
add_filter( 'single_template', 'wpse_category_single_template' );

Here I used single-cat1.php for category id 1 and single-cat2.php for category id 2. You can name these single templates as you feel right.

This function also uses default fallback to single.php if there is no single-cat1.php or single-cat2.php exists.

EDIT 1

I have been using above code on 2 of my websites and it's working fine on latest version of WordPress.

Paste this in your functions.php

function show_template() {
    global $template;
    print_r($template);
}
add_action( 'wp_head', 'show_template' );

This will print the template file used on each page/post. Now access your website and see if correct template file is being used or not? If it's still showing single.php then there is something wrong with your code.

EDIT 2

Here is your code for that.

function wpse_category_single_template( $single_template ) {
    global $post;
    $all_cats = get_the_category();

    if ( in_category(6) ) {
        if ( file_exists(get_template_directory() . "/page-custom.php") ) {
          return get_template_directory() . "/page-custom.php";
        } else {
          return get_template_directory() . "/page.php";
        }
    }
    return $single_template;
}
add_filter( 'single_template', 'wpse_category_single_template' );

Use filter single_template to change template file and function in_category to check is this post in category.

add_filter( 'single_template', 'my_single_template' );
function my_single_template($single_template)
{
    if (in_category(1)) {
        $file = get_template_directory().'/single-cat-1.php';
        if ( file_exists($file) ) {
            return $file;
        }
    }
    return $single_template;
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.