2013-01-21 122 views
1

我已经为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'); 

但作为一个插件开发者,我想在我的插件目录中不是手动创建新模板。我应该怎么做?

回答

4

我最近做了一些像这样使用“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的模板文件。这是为自定义帖子类型创建单个和存档模板。

相关问题