简体   繁体   中英

Wordpress custom post type with categories archive page

I'm struggling to get my custom post type to use my custom archive template, hopefully someone can see where I'm going wrong and help me get back on track please?

Here is the code I've used to create the custom post type:

add_action( 'init', 'news_post_type' );
function news_post_type() {
    register_post_type( 'news',
    array(
      'labels' => array(
        'name' => __( 'News' ),
        'singular_name' => __( 'News' )
      ),
      'capability_type' =>  'post',
      'has_archive' => true,
      'hierarchical' => true,
      'public' => true,
      'supports' => array('title','editor','excerpt','trackbacks','custom-fields','revisions','thumbnail','author','page-attributes',)
    )
  );
}
register_taxonomy( 'news_category', 'news',array('label' => __( 'Categories' ),'rewrite' => array( 'slug' => 'news/category' ),'hierarchical' => true, ) );

Which is great and the URL returns: www.mysite.com/news/category/%the_category% just like I want.

The problem is that I want each of the categories in this CPT to use my custom template, but when I create a file called archive-news.php it is ignored. However if I create a file called archive.php then it works but obviously this is applied to all other post archives which I don't want.

Am I not naming the template file correctly? Is there an error in how I've created the CPT?

If anyone can help that would be greatly appreciated.

Many thanks

i have check with your code as its working properly on my side. i think you checking on detail page which single-$posttype.php file.

Using taxonomy template worked in this instance, so naming my file taxonomy-news_category.php allowed me to create my own template for all categories in my custom post type.

I also found that having a page named the same as the custom post type was causing issues too. So in summary, rename the page or use the taxonomy template as above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM