admin管理员组文章数量:1416627
The documentation for template hierarchy for custom post types (CPT) includes:
- Either for the CPT itself (
archive-cptname.php
) - Or for custom taxonomies (
taxonomy-taxonomyname.php
) - 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:
- Either for the CPT itself (
archive-cptname.php
) - Or for custom taxonomies (
taxonomy-taxonomyname.php
) - 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?
1 Answer
Reset to default 2Because 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.
本文标签: custom post typeglobal categorieswhat’s the template name
版权声明:本文标题:Custom post type, global categories — what’s the template name? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745256684a2650145.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论