我正在构建一个Wordpress插件,该插件添加了一个自定义的帖子类型,我想为其添加一个默认的显示模板。本质上,这是一个事件管理插件,自定义的帖子类型用于事件。有一些自定义元字段,以及一个子帖子类型(性能),因此如果没有默认模板来显示它们,使用它将非常不友好。但如果需要,我希望主题设计师能够为这些帖子类型创建自己的模板。
除非主题提供自己的模板,否则有没有办法使用插件提供的模板?这样做的最佳做法是什么?
Edit:
按照Peter Rowell的建议,我捕获了template\\u重定向操作,如果帖子类型是我的,并且当前主题中不存在该类型的模板,则默认为插件的模板:class FestivityTemplates {
public static function determineTemplate(){
global $post;
$standard_type = strpos($post->post_type, \'fest_\');
if(is_single() && $standard_type !== false) {
FestivityTemplates::loadSingleTemplate($post);
}
}
private static function loadSingleTemplate($post) {
$template_name = \'single-\'.$post->post_type.\'.php\';
$template = locate_template(array($template_name), true);
if(empty($template)) {
include(WP_PLUGIN_DIR . \'/Festivity/lib/templates/\' . $template_name);
exit();
}
}
}
add_action(\'template_redirect\', array(\'FestivityTemplates\', \'determineTemplate\'));