谢谢你的帮助。为了使用当前的WordPress版本,我稍微修改了代码。还将其更改为支持多个自定义模板。
我打赌有更好的方法可以做到这一点,但它对我很有效。
/**
* Load Template with Plugin
*/
function yourname_add_page_template ($templates) {
$templates[\'page-one.php\'] = \'title here One\';
$templates[\'page-two.php\'] = \'title here Two\';
$templates[\'page-three.php\'] = \'title here Three\';
return $templates;
}
add_filter (\'theme_page_templates\', \'yourname_add_page_template\');
function yourname_redirect_page_template ($template) {
$post = get_post();
$page_template = get_post_meta( $post->ID, \'_wp_page_template\', true );
if (\'page-one.php\' == basename ($page_template)) {
$template = WP_PLUGIN_DIR . \'/pluginname/templates/page-one.php\';
return $template;
}
elseif (\'page-two.php\' == basename ($page_template)) {
$template = WP_PLUGIN_DIR . \'/pluginname/templates/page-two.php\';
return $template;
}
elseif (\'page-three.php\' == basename ($page_template)) {
$template = WP_PLUGIN_DIR . \'/pluginname/templates/page-three.php\';
return $template;
}
}
add_filter (\'page_template\', \'yourname_redirect_page_template\');