我已经为wordpress插件目录中的自定义页面创建了一个模板文件,但我无法找到正确的路径。这段代码不起作用:
update_post_meta( $pas_tasks_page_id, '_wp_page_template', dirname( __FILE__ ) . '/task-list-template.php' );
仅当我将模板文件手动放入wordpress主题并将代码更改为:
时,它才有效update_post_meta( $pas_tasks_page_id, '_wp_page_template', '/task-list-template.php' );
但作为插件开发人员,我想在我的插件目录中创建新模板而不是手动创建。我该怎么做?
答案 0 :(得分:5)
我最近使用“template_include”过滤器做了类似的事情。我是这样做的:
function include_template_files() {
$plugindir = dirname( __FILE__ );
if (is_post_type_archive( 'post-type-name' )) {
$templatefilename = 'archive-post_type_name.php';
$template = $plugindir . '/theme_files/' . $templatefilename;
return $template;
}
if ('post-type-name' == get_post_type() ){
$templatefilename = 'single-post-type-name.php';
$template = $plugindir . '/theme_files/' . $templatefilename;
return $template;
}
}
add_filter( 'template_include', 'include_template_files' );
我只是使用wordpress条件来检查所请求的模板是什么,然后在我的插件目录中创建了一个“theme_files”文件夹,并在其中放置了相应命名的wordpress模板文件。这是为自定义帖子类型创建单个和归档模板。