Custom post type, global categories — what’s the template name?

The documentation for template hierarchy for custom post types (CPT) includes: Either for the CPT itself (archive-cptnam

The documentation for template hierarchy for custom post types (CPT) includes:

  1. Either for the CPT itself (archive-cptname.php)
  2. Or for custom taxonomies (taxonomy-taxonomyname.php)
  3. Or for overall categories (category-catname.php)

But it doesn't include the obvious "CPT, with global categories". What would that be? I tried something like archive-cptname-categoryname.php, but this doesn't work. Any pointers?

The documentation for template hierarchy for custom post types (CPT) includes:

  1. Either for the CPT itself (archive-cptname.php)
  2. Or for custom taxonomies (taxonomy-taxonomyname.php)
  3. Or for overall categories (category-catname.php)

But it doesn't include the obvious "CPT, with global categories". What would that be? I tried something like archive-cptname-categoryname.php, but this doesn't work. Any pointers?

Share Improve this question asked Aug 8, 2019 at 20:28 Khom NazidKhom Nazid 17110 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

Because there's no such thing.

If you use the same taxonomy for two or more post types then there isn't separate archives for each post type in that taxonomy's terms. There's a single archive for that taxonomy that lists posts of both post types. This might not work for built in taxonomies (categories and tags) though, as they are configured to only display posts. To display your post type on the category archive you'll need to use the pre_get_posts filter to add it:

add_filter(
    'pre_get_posts',
    function( $query ) {
        if ( $query->is_category() ) {
            $query->set( 'post_type', [ 'post', 'cptname' ] );
        }
    }
);

If you need separate archives for categories for each post type, then you need to register a separate taxonomy for your post type. Something like cptname_category. They won't share terms, but they will have separate archives.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745256684a4619000.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信