admin管理员组文章数量:1410674
I've created a Custom Post Type and I want to theme it. I want to apply a Page Template to instances of my Custom Post Type.
I want to avoid creating a child theme.
Ideally I'd like to create a Page Template within a plugin, and have the template be applied to the Custom Post Type automatically.
How do I do this?
I've created a Custom Post Type and I want to theme it. I want to apply a Page Template to instances of my Custom Post Type.
I want to avoid creating a child theme.
Ideally I'd like to create a Page Template within a plugin, and have the template be applied to the Custom Post Type automatically.
How do I do this?
Share Improve this question edited Nov 20, 2019 at 5:42 Mayeenul Islam 12.9k21 gold badges85 silver badges169 bronze badges asked Nov 20, 2019 at 3:52 bob.dobbsbob.dobbs 1234 bronze badges 7 | Show 2 more comments1 Answer
Reset to default 1Here you are - a plugin that is registering a Custom Post Type (CPT) and pushing template file from the plugin for that particular CPT.
<?php
/**
* Plugin Name: WPSE352898 Template Test
* Author: Mayeenul Islam
* Author URI: https://mayeenulislam.github.io
*/
/**
* Register My Custom Post Type.
*/
function wpse352898_register_cpt() {
register_post_type('mycpt', array(
'labels' => array(
'name' => 'My CPT',
'singular_name' => 'My CPT',
'menu_name' => 'My CPT',
),
'public' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'rewrite' => true,
));
}
add_action( 'init', 'wpse352898_register_cpt' );
/**
* Template loader
*
* @param string $template The template that is called.
*
* @return string Template, that is thrown per modification.
*/
function wpse352898_template_loader( $template ) {
$find = array();
$file = '';
if ( is_single() && 'mycpt' === get_post_type() ) {
// Singular template for CPT 'mycpt' in a theme.
$file = 'single-mycpt.php';
$find[] = $file;
// Directory in a theme, where user can define their custom template to override plugin imposed template.
$find[] = 'mycpt-templates/' . $file;
}
if ( $file ) {
$template = locate_template( array_unique( $find ) );
if ( ! $template ) {
// Load template file from the plugin under this directory.
$template = untrailingslashit( plugin_dir_path( __FILE__ ) ) . '/templates/' . $file;
}
}
return $template;
}
add_filter( 'template_include', 'wpse352898_template_loader' );
After activating the plugin you will need to flush rewrite rules manually by saving the form in Settings » Permalinks without any modification. But you can also flush the rewrite rules from the plugin on
register_activation_hook
(which is not shown here).
To display a template file from the plugin the plugin will look into the template here: <plugin>/templates/single-mycpt.php
.
There will be provision to override the plugin imposed template from the active theme, where it will look into the overridden template here: <active-theme>/mycpt-templates/single-mycpt.php
.
Hope it helps. :)
本文标签: Apply a custom Template to a Custom Post Type from within a plugin
版权声明:本文标题:Apply a custom Template to a Custom Post Type from within a plugin 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744988780a2636253.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
the_content
filter to output your post type template. – Jacob Peattie Commented Nov 20, 2019 at 4:53