# Create Category Template for Custom Post Type

**URL:** <https://discourse.roots.io/t/create-category-template-for-custom-post-type/421>\
**Category:** uncategorized\
**Created:** [September 30, 2013, 9:31am UTC](https://discourse.roots.io/t/create-category-template-for-custom-post-type/421 "2013-09-30T09:31:20Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nick](https://discourse.roots.io/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@Nick](https://discourse.roots.io/u/Nick)\
**Post date:** [September 30, 2013, 9:31am UTC](https://discourse.roots.io/t/create-category-template-for-custom-post-type/421/1 "2013-09-30T09:31:21Z")

</div>

I created a custom category template for a custom post type by using a base-[category].php calling category-[category].php for the category template.

All of that works fine but if you click on a post the content that shows is the content from the category content. So it doesn’t show what should be in the single content. It shows the content from the loop in the category.

What is the best way about creating custom category templates and custom post templates for custom post types?

---

<div class="post-metadata">

**Author:** ![Nick](https://discourse.roots.io/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@Nick](https://discourse.roots.io/u/Nick)\
**Post date:** [October 1, 2013, 8:18am UTC](https://discourse.roots.io/t/create-category-template-for-custom-post-type/421/2 "2013-10-01T08:18:22Z")

</div>

I figured it out. For custom post types using category-[slug].php wasn’t working. So I used archive-[posttype].php and it worked.

---

<div class="post-metadata">

**Author:** ![Rick](https://discourse.roots.io/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@Rick](https://discourse.roots.io/u/Rick)\
**Post date:** [October 11, 2013, 10:15am UTC](https://discourse.roots.io/t/create-category-template-for-custom-post-type/421/3 "2013-10-11T10:15:28Z")

</div>

I have created a custom post type Partnersearch (with archive).  
to customize the singel I created single-partnersearch.php and now I want to customize the archive partnersearchpage.  
I made archive-partnersearch and this works but it doesn’t target the base.

How can I target the base for this archive page?

---

<div class="post-metadata">

**Author:** ![Nick](https://discourse.roots.io/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@Nick](https://discourse.roots.io/u/Nick)\
**Post date:** [October 11, 2013, 11:09am UTC](https://discourse.roots.io/t/create-category-template-for-custom-post-type/421/4 "2013-10-11T11:09:53Z")

</div>

For that you can use base-[category].php. Or you can just use conditionals on base.php.

---

<div class="post-metadata">

**Author:** ![Rick](https://discourse.roots.io/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@Rick](https://discourse.roots.io/u/Rick)\
**Post date:** [October 11, 2013, 12:07pm UTC](https://discourse.roots.io/t/create-category-template-for-custom-post-type/421/5 "2013-10-11T12:07:55Z")

</div>

Thats what i did base-partnersearch.php but that doesn’t work it opens the archive page in the base.php

---

<div class="post-metadata">

**Author:** ![Foxaii](https://discourse.roots.io/user_avatar/discourse.roots.io/foxaii/32/6289_2.png) [@Foxaii](https://discourse.roots.io/u/Foxaii)\
**Post date:** [October 11, 2013, 12:28pm UTC](https://discourse.roots.io/t/create-category-template-for-custom-post-type/421/6 "2013-10-11T12:28:14Z")

</div>

Use base-archive-partnersearch.php.

---

<div class="post-metadata">

**Author:** ![Rick](https://discourse.roots.io/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@Rick](https://discourse.roots.io/u/Rick)\
**Post date:** [October 11, 2013, 1:21pm UTC](https://discourse.roots.io/t/create-category-template-for-custom-post-type/421/7 "2013-10-11T13:21:31Z")

</div>

uhm strange I have done that but not working.

base-archive-partnersearch.php is a 3 column structure and base 2 column  
as you can see its still using base  
﻿http://urbaneurope.studio-pit.nl/partnersearch/

---

<div class="post-metadata">

**Author:** ![Foxaii](https://discourse.roots.io/user_avatar/discourse.roots.io/foxaii/32/6289_2.png) [@Foxaii](https://discourse.roots.io/u/Foxaii)\
**Post date:** [October 11, 2013, 2:43pm UTC](https://discourse.roots.io/t/create-category-template-for-custom-post-type/421/8 "2013-10-11T14:43:06Z")

</div>

Double check the spelling of your files and make sure there isn’t any logic in your custom base preventing the 3 column layout.

---

<div class="post-metadata">

**Author:** ![Rick](https://discourse.roots.io/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@Rick](https://discourse.roots.io/u/Rick)\
**Post date:** [October 14, 2013, 10:36am UTC](https://discourse.roots.io/t/create-category-template-for-custom-post-type/421/9 "2013-10-14T10:36:17Z")

</div>

vert strange, I checked everything everything seems to be good.

I already made a 3 column structure for base-single-projects.php and that one is working perfect.  
[http://urbaneurope.studio-pit.nl/projects/project-gamba/](http://urbaneurope.studio-pit.nl/projects/project-gamba/)  
Don’t get it why it aint working for archive

---

<div class="post-metadata">

**Author:** ![Foxaii](https://discourse.roots.io/user_avatar/discourse.roots.io/foxaii/32/6289_2.png) [@Foxaii](https://discourse.roots.io/u/Foxaii)\
**Post date:** [October 14, 2013, 3:56pm UTC](https://discourse.roots.io/t/create-category-template-for-custom-post-type/421/10 "2013-10-14T15:56:39Z")

</div>

I haven’t been able to replicate the issue.

The content template (chosen by WordPress) should be in: `Roots_Wrapping::$main_template`

The base file template (chosen by the wrapper) should be stored in the global `$template`.

You can also manually override the base file with the `roots_wrap_base` filter if you just need it fixed.

---

<div class="post-metadata">

**Author:** ![Rick](https://discourse.roots.io/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@Rick](https://discourse.roots.io/u/Rick)\
**Post date:** [October 14, 2013, 5:06pm UTC](https://discourse.roots.io/t/create-category-template-for-custom-post-type/421/11 "2013-10-14T17:06:43Z")

</div>

Nick thanks for the help so far, your suggestions are a little bit out of my league.  
Is it too much to ask you to take a look at my files? I’m not sure if this is the appropriate setting to ask, but I could use some more help.

---

<div class="post-metadata">

**Author:** ![Foxaii](https://discourse.roots.io/user_avatar/discourse.roots.io/foxaii/32/6289_2.png) [@Foxaii](https://discourse.roots.io/u/Foxaii)\
**Post date:** [October 14, 2013, 5:37pm UTC](https://discourse.roots.io/t/create-category-template-for-custom-post-type/421/12 "2013-10-14T17:37:33Z")

</div>

Put the following in your `lib/custom.php` or `functions.php` file. It will print out what templates you are using in the footer, which may help you out.

```php
// Bug testing only. Not to be used on a production site!!
add_action('wp_footer', 'roots_wrap_info');

function roots_wrap_info() {
  $format = '<h6>The %s template being used is: %s</h6>';
  $main = Roots_Wrapping::$main_template;
  global $template;

  printf($format, 'Main', $main);
  printf($format, 'Base', $template);
}

```

The files are unlikely to help much. The directory structure may help. The base file and the archive file both need to be in the root.

---

<div class="post-metadata">

**Author:** ![Rick](https://discourse.roots.io/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@Rick](https://discourse.roots.io/u/Rick)\
**Post date:** [October 30, 2013, 12:56pm UTC](https://discourse.roots.io/t/create-category-template-for-custom-post-type/421/13 "2013-10-30T12:56:31Z")

</div>

I changed the structure and deleted the custom posttype. I have made two categories projects and partnersearch.  
I created single-projects how can i target the base I tried base-single-projects but no results don’t know if it works that way for singles?

---

<div class="post-metadata">

**Author:** ![Foxaii](https://discourse.roots.io/user_avatar/discourse.roots.io/foxaii/32/6289_2.png) [@Foxaii](https://discourse.roots.io/u/Foxaii)\
**Post date:** [October 30, 2013, 1:31pm UTC](https://discourse.roots.io/t/create-category-template-for-custom-post-type/421/14 "2013-10-30T13:31:26Z")

</div>

The WordPress template hierarchy doesn’t support the `single-{$category}.php` format so you’ll need a function to do that for you. You should be good to go when that’s set up; the wrapper always prepends `base-` to the template selected by WordPress.

---

<div class="post-metadata">

**Author:** ![DriesP](https://discourse.roots.io/user_avatar/discourse.roots.io/driesp/32/6553_2.png) [@DriesP](https://discourse.roots.io/u/DriesP)\
**Post date:** [December 12, 2013, 7:32pm UTC](https://discourse.roots.io/t/create-category-template-for-custom-post-type/421/15 "2013-12-12T19:32:07Z")

</div>

If you’re looking for a good function to create `single-{$category}.php` Check out this code I found on ( [https://halgatewood.com/wordpress-custom-single-templates-by-category/](https://halgatewood.com/wordpress-custom-single-templates-by-category/) )

```
add_filter('single_template', 'check_for_category_single_template');
function check_for_category_single_template( $t )
{
  foreach( (array) get_the_category() as $cat ) 
  { 
    if ( file_exists(TEMPLATEPATH . "/single-category-{$cat->slug}.php") ) return TEMPLATEPATH . "/single-category-{$cat->slug}.php"; 
    if($cat->parent)
    {
      $cat = get_the_category_by_ID( $cat->parent );
      if ( file_exists(TEMPLATEPATH . "/single-category-{$cat->slug}.php") ) return TEMPLATEPATH . "/single-category-{$cat->slug}.php";
    }
  } 
  return $t;
}

```

1. Place this code in /lib/custom.php
2. Add single-category-YOURCATEGORYNAME.php
3. Add base-single-cateogry-YOURCATEGORYNAME.php

Thought I would post this here because it took me a while to figure this out. Just to help y’all!

---

<div class="post-metadata">

**Author:** ![jefff](https://discourse.roots.io/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@jefff](https://discourse.roots.io/u/jefff)\
**Post date:** [April 28, 2015, 9:42pm UTC](https://discourse.roots.io/t/create-category-template-for-custom-post-type/421/16 "2015-04-28T21:42:08Z")

</div>

@Foxaii, is there an updated version of roots\_wrap\_info()? I tried this in extras.php:

```
function sage_wrap_info() {
  $format = '<h6>The %s template being used is: %s</h6>';
  $main = SageWrapping::$main_template;
  global $template;

  printf($format, 'Main', $main);
  printf($format, 'Base', $template);
}
add_action('wp_footer', __NAMESPACE__. '\\sage_wrap_info');
```

---

<div class="post-metadata">

**Author:** ![jefff](https://discourse.roots.io/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@jefff](https://discourse.roots.io/u/jefff)\
**Post date:** [April 28, 2015, 10:01pm UTC](https://discourse.roots.io/t/create-category-template-for-custom-post-type/421/17 "2015-04-28T22:01:15Z")

</div>

Oh, it needs the correct namespace `Roots\Sage\Wrapper`. For anyone interested, I solved it by adding multiple namespaces to `extras.php`: [http://php.net/manual/en/language.namespaces.definitionmultiple.php](http://php.net/manual/en/language.namespaces.definitionmultiple.php)

---

<div class="post-metadata">

**Author:** ![Foxaii](https://discourse.roots.io/user_avatar/discourse.roots.io/foxaii/32/6289_2.png) [@Foxaii](https://discourse.roots.io/u/Foxaii)\
**Post date:** [April 29, 2015, 12:15am UTC](https://discourse.roots.io/t/create-category-template-for-custom-post-type/421/18 "2015-04-29T00:15:08Z")

</div>

I’ve added a branch to the Roots Wrapper Toolbar plugin that [supports Sage](https://github.com/roots/roots-wrapper-toolbar/tree/sagesupport). It’s a lot neater solution to the “which template?” issue you are having.
