admin管理员组文章数量:1429625
I have two custom post types, their slugs are: slug-a
and slug-b
.
I have created two identical single template: single-slug-a.php
and single-slug-b.php
.
This obviously works as expected.
Is there any way to create just one PHP file for both my post types (ex. single-slug-a-and-slug-b.php
)?
SOLUTION:
I've found this solution:
add_filter( 'template_include', function( $template )
{
// your custom post types
$my_types = array( 'slug-a', 'slug-b' );
$post_type = get_post_type();
if ( ! in_array( $post_type, $my_types ) )
return $template;
return get_stylesheet_directory() . '/single-my-template.php';
});
Is there a more correct way?
I have two custom post types, their slugs are: slug-a
and slug-b
.
I have created two identical single template: single-slug-a.php
and single-slug-b.php
.
This obviously works as expected.
Is there any way to create just one PHP file for both my post types (ex. single-slug-a-and-slug-b.php
)?
SOLUTION:
I've found this solution:
add_filter( 'template_include', function( $template )
{
// your custom post types
$my_types = array( 'slug-a', 'slug-b' );
$post_type = get_post_type();
if ( ! in_array( $post_type, $my_types ) )
return $template;
return get_stylesheet_directory() . '/single-my-template.php';
});
Is there a more correct way?
Share Improve this question edited Apr 26, 2019 at 14:58 aletede91 asked Apr 26, 2019 at 14:52 aletede91aletede91 1012 bronze badges1 Answer
Reset to default 0The other option would be to create the template as a Page Template, and have both CPTs support page-attributes
so they can then use Page Templates.
Typically you name the template something like tpl-yourname.php
and it would contain a comment at the top:
<?php
/*
* Template Name: Your Name
* Template Post Type: cpt-slug-a, cpt-slug-b
*
The template name will be what you see in the dropdown menu when you are editing one of the CPTs. The template post type needs to contain the names of the post types - i.e. a WP Core Post you would specify as "post" and a CPT called Portfolio you would specify as "portfolio".
You also have to tell each CPT to support page-attributes
so that the editor will show the template dropdown.
register_post_type('cpt-slug-a',
// lots of other code here
// this just shows the line you need to add "page-attributes" to
'supports' => array('title', 'editor', 'page-attributes');
// lots of other code here
);
Just add page-attributes
to the array, leaving whatever other things your CPTs support.
As to which way is "correct," I think that's a matter of preference. To me personally this way of using Page Templates seems a little more flexible, since non-technical people can then easily apply the template to whatever pages they like in the editor, and your original way requires coding to apply the template to any additional pages.
本文标签: Two post types with same single template
版权声明:本文标题:Two post types with same single template 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745548952a2662839.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论