我正在开发一个插件。该插件可定制帖子类型“product”。首先我使用template_redirect
重定向到单个页面和类别页面的操作。
以下是我的template\\u重定向代码:
add_action("template_redirect", \'my_theme_redirect\');
function my_theme_redirect() {
global $wp;
$plugindir = dirname(__FILE__);
//A Specific Custom Post Type
if ($wp->query_vars["post_type"] == \'product\') {
$templatefilename = \'single-product.php\';
if (file_exists(TEMPLATEPATH . \'/\' . $templatefilename)) {
$return_template = TEMPLATEPATH . \'/\' . $templatefilename;
} else {
$return_template = $plugindir . \'/themefiles/\' . $templatefilename;
}
do_theme_redirect($return_template);
}
if (is_tax(\'prodcategories\')) {
$templatefilename = \'taxonomy-prodcategories.php\';
if (file_exists(TEMPLATEPATH . \'/\' . $templatefilename)) {
$return_template = TEMPLATEPATH . \'/\' . $templatefilename;
} else {
$return_template = $plugindir . \'/themefiles/\' . $templatefilename;
}
do_theme_redirect($return_template);
}
}
function do_theme_redirect($url) {
global $post, $wp_query;
if (have_posts()) {
include($url);
die();
} else {
$wp_query->is_404 = true;
}
}
它工作得很好。但现在我想用
template_include
过滤器,但它不工作,我的网站变成空白。
以下是template\\u include代码:
add_filter("template_include", \'my_theme_redirect\');
function my_theme_redirect($templatefilename) {
global $wp;
$plugindir = dirname(__FILE__);
//A Specific Custom Post Type
if ($wp->query_vars["post_type"] == \'product\') {
$templatefilename = \'single-product.php\';
if (file_exists(TEMPLATEPATH . \'/\' . $templatefilename)) {
$return_template = TEMPLATEPATH . \'/\' . $templatefilename;
} else {
$return_template = $plugindir . \'/themefiles/\' . $templatefilename;
}
return $return_template;
}
if (is_tax(\'prodcategories\')) {
$templatefilename = \'taxonomy-prodcategories.php\';
if (file_exists(TEMPLATEPATH . \'/\' . $templatefilename)) {
$return_template = TEMPLATEPATH . \'/\' . $templatefilename;
} else {
$return_template = $plugindir . \'/themefiles/\' . $templatefilename;
}
return $return_template;
}
}
function do_theme_redirect($url) {
global $post, $wp_query;
if (have_posts()) {
include($url);
die();
} else {
$wp_query->is_404 = true;
}
}
我哪里出了问题,有什么建议吗
最合适的回答,由SO网友:deemi-D-nadeem 整理而成
好的,我自己做的。
我删除了以上所有代码并编写了此代码。这对我来说非常有效
代码:
function template_chooser($template){
global $wp_query;
$plugindir = dirname(__FILE__);
$post_type = get_query_var(\'post_type\');
if( $post_type == \'product\' ){
return $plugindir . \'/themefiles/single-product.php\';
}
if (is_tax(\'prodcategories\')) {
return $plugindir . \'/themefiles/taxonomy-prodcategories.php\';
}
return $template;
}
add_filter(\'template_include\', \'template_chooser\');
SO网友:Ejaz UL Haq
这项工作对我来说,请尝试一下,感谢模板加载到cpt文件中,该文件位于自定义插件->;应用程序->;cpt->;cpt\\U文章。php模板位于自定义插件->;应用程序->;模板
add_filter( \'template_include\', \'load_my_custom_template\', 99, 1 );
function load_custom_templates($template) {
$post_type = \'article\';
if( is_post_type_archive( $post_type ) && file_exists( plugin_dir_path(__FILE__) .
\'templates/archive_help_lessions.php\' ) ){
$template = trailingslashit( plugin_dir_path( __FILE__ ) .\'app/templates\' ).\'archive_articles.php\';
}
if ( is_singular( $post_type ) ) {
$template = trailingslashit( plugin_dir_path( __FILE__ ) .\'app/templates\' ).\'single_article.php\';
}
return $template;
}