Title: Single.php &#038; Archive.php based on Category
Last modified: August 18, 2016

---

# Single.php & Archive.php based on Category

 *  [bcreighton](https://wordpress.org/support/users/bcreighton/)
 * (@bcreighton)
 * [19 years, 5 months ago](https://wordpress.org/support/topic/singlephp-038-archivephp-based-on-category/)
 * Is there any way to customize the template layout for the Single.php and Archive.
   php pages based on what category it is?
 * I have custom templates made for each category page (category-1.php, etc) and
   I would like the layout to carry over to the individual entry and monthly archives.
 * Any ideas?
 * Thanks!

Viewing 10 replies - 1 through 10 (of 10 total)

 *  Moderator [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [19 years, 5 months ago](https://wordpress.org/support/topic/singlephp-038-archivephp-based-on-category/#post-493690)
 * Not as easily, no. The reason for this is:
    -Single posts can belong to multiple
   categories, so which category template should it use? -Archive pages can have
   multiple posts, so again, which category template should it use?
 * That said, it would be possible to roll your own code to deal with this sort 
   of thing.
 * All templates get run through a filter. In your case, the filters you’re interested
   in are single_template and archive_template. You could do something like this:
 * `function choose_single_template($template) 
    { // $template contains the filename
   of the template being used (like "/single.php") // decide what template you want
   to use $template = "/single-whatever.php";  // send it back return $template;}
   add_filter('single_template','choose_single_template');
 * Basically, the filter gets the filename of the template. You can change it and
   return it and thus force a different template. You would probably want to put
   this sort of code in your theme’s functions.php file.
 *  [Glenn Ansley](https://wordpress.org/support/users/blepoxp/)
 * (@blepoxp)
 * [19 years, 5 months ago](https://wordpress.org/support/topic/singlephp-038-archivephp-based-on-category/#post-493692)
 * You could probably do it with [conditional statements](http://codex.wordpress.org/Conditional_Tags#A_Single_Post_Page)
 * Also read here:
    [http://wordpress.org/support/topic/27073?replies=29](http://wordpress.org/support/topic/27073?replies=29)
 *  [moshu](https://wordpress.org/support/users/moshu/)
 * (@moshu)
 * [19 years, 5 months ago](https://wordpress.org/support/topic/singlephp-038-archivephp-based-on-category/#post-493696)
 * For the single:
    [http://guff.szub.net/2005/07/21/post-templates-by-category/](http://guff.szub.net/2005/07/21/post-templates-by-category/)
 * With the monthly archives is more complicated because WP doesn’t filter posts
   by month AND category.
 *  Thread Starter [bcreighton](https://wordpress.org/support/users/bcreighton/)
 * (@bcreighton)
 * [19 years, 5 months ago](https://wordpress.org/support/topic/singlephp-038-archivephp-based-on-category/#post-493701)
 * Thanks!
 * I believe this is what I’m looking for, there will be only one category per post.
 * Now I just need to wrap my brain around it –
 * this filter would be declared in template-loader.php?
 * How would I pass the category? Maybe something like this:
 * `} else if ( is_single() && $template = get_single_template($mcat) ) {
    if ( 
   is_attachment() ) add_filter('the_content', 'prepend_attachment'); include($template);
   exit; }
 * `function get_single_template($catid) {
    $template = TEMPLATEPATH . '/single-'
   $catid.php; return $template;  }
 *  Thread Starter [bcreighton](https://wordpress.org/support/users/bcreighton/)
 * (@bcreighton)
 * [19 years, 5 months ago](https://wordpress.org/support/topic/singlephp-038-archivephp-based-on-category/#post-493702)
 * Thanks, blepoxp – I thought about using one big giant conditional for each of
   the pages (it’s my backup solution).
 * moshu – I’ll have to take a look at that and digest it a bit, thanks!.
 *  [moshu](https://wordpress.org/support/users/moshu/)
 * (@moshu)
 * [19 years, 5 months ago](https://wordpress.org/support/topic/singlephp-038-archivephp-based-on-category/#post-493703)
 * That’s a plugin, you don’t have to “digest” anything – just install it and your
   single posts will work with the category templates.
    There is also another plugin
   mentioned on that page: the “inheritor” 🙂
 *  Thread Starter [bcreighton](https://wordpress.org/support/users/bcreighton/)
 * (@bcreighton)
 * [19 years, 5 months ago](https://wordpress.org/support/topic/singlephp-038-archivephp-based-on-category/#post-493708)
 * Yep, already working! 🙂 I’ll check out the inheritor – thanks!!
 *  Thread Starter [bcreighton](https://wordpress.org/support/users/bcreighton/)
 * (@bcreighton)
 * [19 years, 5 months ago](https://wordpress.org/support/topic/singlephp-038-archivephp-based-on-category/#post-493713)
 * No, Inheritor doesn’t help at all. I’m just going to pass the cat id via the 
   url string and do some conditional statements.
 * Thanks everyone!
 *  [desmondo](https://wordpress.org/support/users/desmondo/)
 * (@desmondo)
 * [18 years, 10 months ago](https://wordpress.org/support/topic/singlephp-038-archivephp-based-on-category/#post-494045)
 * Hey, that Plugin works perfectly here, thanks Moshu
 *  [desmondo](https://wordpress.org/support/users/desmondo/)
 * (@desmondo)
 * [18 years, 10 months ago](https://wordpress.org/support/topic/singlephp-038-archivephp-based-on-category/#post-494046)
 * One question I have, how would you change the title on the category template 
   so it would change when you are in the archive section to “Archives” or something
   like that?

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘Single.php & Archive.php based on Category’ is closed to new replies.

## Tags

 * [archive.php](https://wordpress.org/support/topic-tag/archive-php/)
 * [custom](https://wordpress.org/support/topic-tag/custom/)
 * [single.php](https://wordpress.org/support/topic-tag/single-php/)

 * 10 replies
 * 5 participants
 * Last reply from: [desmondo](https://wordpress.org/support/users/desmondo/)
 * Last activity: [18 years, 10 months ago](https://wordpress.org/support/topic/singlephp-038-archivephp-based-on-category/#post-494046)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
