这是this question/answer. 关于这个问题的评论提到template_include
优先于template_redirect
从插件加载模板时。
但是,将此挂起到template\\u include会破坏网站上的所有内容(任何其他页面视图),但使用布局替代的布局除外http://pastebin.com/LWpSrfim 但是使用template\\u include只预先添加内容(到放置在主题目录覆盖中的任何模板文件)——例如,下面仍然输出标准的单一视图。
编辑:源代码
<?php
class moulding_templates {
public static function determine_template(){
global $post,$wp_query;
// custom post types
$post_type_array = array(\'moulding_profiles\',\'moulding_combination\',\'moulding_collection\',\'idea_gallery\');
foreach ($post_type_array as $post_type_single) {
global $post;
if (is_singular($post_type_single)) {
moulding_templates::loadSingleTemplate($post);
}
}
// custom taxonomies
$taxonomy_array = array(\'profile_categories\',\'combination_categories\',\'wood_types\');
foreach ($taxonomy_array as $taxonomy_single) {
if (is_tax($taxonomy_single)) {
moulding_templates::loadTaxonomyTemplate($taxonomy_single);
}
}
}
private static function loadSingleTemplate($post) {
$template_name = \'moulding-templates/single-\'.$post->post_type.\'.php\';
$template = locate_template(array($template_name), true);
if(empty($template)) {
include(MOULDINGS_BASE_DIR . \'includes/\' . $template_name);
exit();
}
}
private static function loadTaxonomyTemplate($taxonomy) {
$template_name = \'moulding-templates/taxonomy-\'.$taxonomy.\'.php\';
$template = locate_template(array($template_name), true);
if(empty($template)) {
include(MOULDINGS_BASE_DIR . \'includes/\' . $template_name);
exit();
}
}
}
add_action(\'template_include\', array(\'moulding_templates\', \'determine_template\'));