admin管理员组

文章数量:1434893

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?

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 edited Feb 22, 2011 at 18:00 keatch 2,5952 gold badges24 silver badges25 bronze badges asked Feb 22, 2011 at 17:47 Kelvin JayanorisKelvin Jayanoris 1281 gold badge1 silver badge6 bronze badges 1
  • I got a solution 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. – Kiran Kumar Kadavakollu Commented Apr 4, 2019 at 8:34
Add a comment  | 

4 Answers 4

Reset to default 6

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/Template_Hierarchy#Custom_Taxonomies_display

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

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"); ?>

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)

本文标签: categoriesCustom Post Types on Category Pages