I am trying to create a single.php file for a specific category on my site. Instead of loading the single.php file I want WP to return that category in a file called single-category-53.php (the actual category id=53). I allow the selection of multiple categories on the site, and with the help of Hikari's permalink plugin, we choose the appropriate category for the permalink. Because of this I can't reference any built in functions for categories as Wordpress defaults to a numerical order. The code below works through the if ($catp == 53)... I've verified that. The issue seems to be in calling the single-category-53.php file, or I'm improperly doing the nested if statement.

I have verified that I uploaded the single-category-53.php file to the child theme directory.

Any help in correction of the code is appreciated!

    // A function which displays a specific catergory equal to the _category_permalink

    function ag_templates($single_template) {

    global $wpdb;
    $table = $wpdb->prefix . "postmeta" ;
    $query = $wpdb->prepare( "SELECT * FROM {$table}
                              WHERE `post_id` = %d 
                              AND `meta_key` LIKE '_category_permalink' ",
                              $post->ID );
    $perma = $wpdb->get_row( $query );
    $catp = $perma->meta_value;
    if ($catp == 53) {
    if ( file_exists (STYLESHEETPATH . "/single-category-53.php") ) 
    $single_template = STYLESHEETPATH . "/single-category-53.php"; }
    return $single_template;

    add_filter( "single_template", "ag_templates" ) ;

    // End specific category template function.
  • I'm confused. you want a particular single template to be used if the post is in a specific category? is it the case that sometimes a post is in that category but you don't want that specific single template used?
    – Milo
    Feb 5, 2013 at 3:42
  • I'm trying to save myself from having to edit 50,000+ posts... :-) We could simply remove the multiple categories and use just one (the one we now permalink). But basically what you're saying is what I need, it would resolve us from having to edit those posts.
    – perdrix
    Feb 5, 2013 at 3:56

2 Answers 2


I think this does what you want:

function ag_templates( $template = '' ){
    global $post;
    $meta = get_post_meta( $post->ID, '_category_permalink', true );
    if( 53 == $meta ){
        $template = locate_template(
            array( "single-category-53.php", $template ),
    return $template;
add_filter( 'single_template', 'ag_templates' );
  • 1
    Milo... you nailed it. Worked like a charm. Thanks!
    – perdrix
    Feb 5, 2013 at 4:08

Don't know why you need it done that way, but if the way doesn't matter too much to you. Here's what I would do.

  1. Create a file called e.g. mycat-term.php
  2. Use a condition to find that category you'r looking for: http://codex.wordpress.org/Function_Reference/is_tax
  3. If that condition then is true I call my file from step one http://codex.wordpress.org/Function_Reference/get_template_part

Put this code in your single.php:

if ( is_category( 'term_you_are_looking_for' ) ) {
    get_template_part( 'mycat', 'term' ); // gets mycat-term.php

Update: I'm not sure I understand what you are trying to acomplish but since you are on single.php and the categories/tags aren't directly available here are a couple more snippets that can help you get them there.

global $post;
$current_tax = get_post_taxonomies($post->ID); // get current taxonomies array: [0] = cat, [1] = tag    
$terms = wp_get_post_terms($post->ID, $current_tax[1]);
$term = $terms->taxonomy;`

You can reference the taxonomy the in the $args for the get_category(); function. http://codex.wordpress.org/Function_Reference/get_categories

  • Marcup... doesn't your system only work if the page is an archive page? I'm not trying to display a template for an archive page... this is a template for the posts within a specific category. It replaces the single.php for that category only.
    – perdrix
    Feb 5, 2013 at 3:40
  • Just updated above.
    – Marc Wiest
    Feb 5, 2013 at 3:54
  • I think your solution would work well marcup if it wasn't for the selection of multiple categories. Since I select a primary category from the multiple categories, I only want to display the page when the category I selected is primary... hence the reason for the call to wpdb.
    – perdrix
    Feb 5, 2013 at 3:57
  • Sorry, that's all I got for you right now.
    – Marc Wiest
    Feb 5, 2013 at 4:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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