Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm working with a custom post type that uses the normal WordPress categories.
I am able to modify my category template to display my custom post types exactly how I want them.

The problem is, once I do this, normal blog posts are also affected.

Of course I can limit either normal blog posts or the post type to just one category and create a category-ID.php to work with just that category.

But I am wondering whether there is a more flexible solution?
Is it possible to make category templates that are specific for post types i.e. one to show posts, one to show custom post types, etc?

is this possible at all? If not, what is the best way around this?

share|improve this question

4 Answers 4

You can based on the post type like so: say you have category.php for regular posts and your-custom-type-category.php for your custom post type posts then just add this to your regular category.php at the very top.

<?php if (have_posts()){
    the_post();
    if('your_custom_type' == get_post_type( $post ) {
        rewind_posts();
        get_template_part('your-custom-type-category');
        exit();
    }
    rewind_posts();
}
?>

and now when ever a category is requested it will check for your custom post type and if found it will use your your-custom-type-category.php as template and all regular posts will display using category.php with no change.

Hope this helps

share|improve this answer
    
Thanks , let me try this and report back to you! –  Kelvin Jayanoris Feb 23 '11 at 15:47

For custom post types with custom taxonomy you can make a template for that taxonomy. Your custom taxonomy template would be named taxonomy-{taxonomy}.php. For example, if your taxonomy was called "cooking-categories" you would name your file taxonomy-cooking-categories.php.

More information on template files for special taxonomy archives can be found in the codex here: http://codex.wordpress.org/Template_Hierarchy#Custom_Taxonomies_display

share|improve this answer

Hi @Kelvin Jayanoris:

Is there a reason why you can't just test for the value of $post->post_type with an if..elseif statement within your Loop, like this:

<?php if ($post->post_type=='foo'): ?>

   Put HTML for 'foo' post type here...

<?php else if ($post->post_type=='bar'): ?>

   Put HTML for 'bar' post type here...

<?php else if ($post->post_type=='post'): ?>

   Put HTML for 'post' post type here...

<?php endif; ?>

Or if there a reason you can't create your own template filename hierarchy in the form of filenames like post-foo.php, post-foo.php, post-post.php, also within your Loop? This one like of code could do that for it:

<?php include("post-{$post->post_type}.php"); ?>
share|improve this answer

An easier answer if you didn't want to create new files, or go through the trouble of messing up your queries is to use this in your functions.php:

function sql_where_clause($sql) {
  if (is_category()) {
    $sql = str_replace("cms_posts.post_type = 'post'", "cms_posts.post_type='post' OR cms_posts.post_type = 'news' OR cms_posts.post_type='event'", $sql); 
  }
  return $sql;
}
add_filter('posts_where', 'sql_where_clause');

of course, you'll need to modify it so that it has your custom post types in with it. I did this so that my category.php would respond to all the custom post types i wanted as if they should normally be there. Saves a lot of trouble.

Edit: Be wary of what version of WP you use this on. It would appear that older versions of WP used wp_posts instead of cms_posts, so verify what table names you're looking to use or you'll end up with odd results (nothing showing up, basically)

share|improve this answer
    
WordPress has never, as far as my memory stretches, used cms_ by default and has always used wp_, though that prefix can be changed on a site by site basis. The correct thing is to use $wpdb->posts rather than guess at any particular prefix. –  s_ha_dum Jun 5 '13 at 12:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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