Create Category Template for Custom Post Type

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?

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

2 Likes

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?

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

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

Use base-archive-partnersearch.php.

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/

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

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/
Don’t get it why it aint working for archive

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.

1 Like

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.

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.

// 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.

3 Likes

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?

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.

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/ )

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!

3 Likes

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

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

I’ve added a branch to the Roots Wrapper Toolbar plugin that supports Sage. It’s a lot neater solution to the “which template?” issue you are having.

1 Like