Tar*_*nyk 2 wordpress plugins wordpress-plugin
我已经为wordpress插件目录中的自定义页面创建了一个模板文件,但我找不到正确的路径.这段代码不起作用:
update_post_meta( $pas_tasks_page_id, '_wp_page_template', dirname( __FILE__ ) . '/task-list-template.php' );
Run Code Online (Sandbox Code Playgroud)
仅当我将模板文件手动放入wordpress主题并将代码更改为:
update_post_meta( $pas_tasks_page_id, '_wp_page_template', '/task-list-template.php' );
Run Code Online (Sandbox Code Playgroud)
但作为插件开发人员,我想在我的插件目录中创建新模板而不是手动创建.我该怎么办?
小智 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' );
Run Code Online (Sandbox Code Playgroud)
我只是使用wordpress条件来检查所请求的模板是什么,然后在我的插件目录中创建了一个"theme_files"文件夹,并在其中放置了相应命名的wordpress模板文件.这是为自定义帖子类型创建单个和归档模板.
| 归档时间: |
|
| 查看次数: |
1840 次 |
| 最近记录: |