我有两种自定义帖子类型(例如post_type_1和post_type_2),我想将其重定向到独立模板(single-post_type_1.php和single-post_type_2.php)来处理它们的显示。我不想将显示模板放在主题文件夹中,因为我希望它们独立包含在各自的插件文件夹中。
如何让他们每个人注册一个template_redirect钩子而不影响另一个?还是我应该使用其他技术?
当前,我正在插件1中进行此操作:
add_action( 'template_redirect', 'template_redirect_1' );
function template_redirect_1() {
global $wp_query;
global $wp;
if ( $wp_query->query_vars['post_type'] === 'post_type_1' ) {
if ( have_posts() )
{
include( PATH_TO_PLUGIN_1 . '/views/single-post_type_1.php' );
die();
}
else
{
$wp_query->is_404 = true;
}
}
}
而在插件2中:
add_action( 'template_redirect', 'template_redirect_2' );
function template_redirect_2() {
global $wp_query;
global $wp;
if ( $wp_query->query_vars['post_type'] === 'post_type_2' ) {
if ( have_posts() )
{
include( PATH_TO_PLUGIN_2 . '/views/single-post_type_2.php' );
die();
}
else
{
$wp_query->is_404 = true;
}
}
}
一旦我注册了插件2的template_redirect钩子,插件1便不再起作用。
我想念什么吗?
做这个的最好方式是什么?