0

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.

1
  • 2
    Or you could use just the single.php and in the contents, you adjust based on an if statement (i.e. <?php if ( in_category(20) ) { ?>).
    – Aibrean
    Aug 5 '15 at 14:05
3

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' );
15
  • Don't use TEMPLATEPATH. See this Nov 18 '14 at 13:33
  • And better use locate_template() instead of "if file_exists() then return"
    – cybmeta
    Nov 18 '14 at 13:38
  • @PieterGoosen just made that change. Although ticket says TEMPLATEPATH is deprecate but it does not show any warnings in WordPress. Thanks :)
    – Robert hue
    Nov 18 '14 at 13:39
  • Thanks guys! I tried to apply this code and it still not works for me. After I add this function to my function.php and defined the single-cat.php, the related pages (with same cat ID) remained unchanged with the basic single.php template. Any ideas why?
    – Netsh
    Nov 18 '14 at 19:10
  • 1
    Why not use in_category() or has_category() to test a post for a specific category attached ;-) Jan 1 '16 at 20:15
2

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;
}
1
  • 2
    Could I get a feedback? What is wrong with my answer? If you downvote please add a comment.
    – Marcin
    Nov 19 '14 at 6:47
1

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;
});
0

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' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.