在子文件夹中动态创建带有模板的页面


dynamically creating pages with templates in a subfolder

我使用以下代码在主题激活时创建页面。当页面模板位于主题的根目录中时,一切都很好。然而,我试图从"页面模板"目录(root->pagetemplates)中引用我的模板文件。我做错了什么?

if (isset($_GET['activated']) && is_admin()){
    add_action('init', 'create_initial_pages');
}
function create_initial_pages() {    
$pages = array(
     // Page Title and URL (a blank space will end up becomeing a dash "-")
    'Services' => array(
        // Page Content     // Template to use (if left blank the default template will be used)
        'Services Content'=>'page-bottom-sidebar.php'),
);
foreach($pages as $page_url_title => $page_meta) {
        $id = get_page_by_title($page_url_title);
    foreach ($page_meta as $page_content=>$page_template){
    $page = array(
        'post_type'   => 'page',
        'post_title'  => $page_url_title,
        'post_name'   => $page_url_title,
        'post_status' => 'publish',
        'post_content' => $page_content,
        'post_parent' => ''
    );
    if(!isset($id->ID)){
        $new_page_id = wp_insert_post($page);
        if(!empty($page_template)){
                update_post_meta($new_page_id, '_wp_page_template', $page_template);
        }
    }
 }
};
}

非常感谢您的帮助。我知道它很简单,就是看不见。

当页面模板位于子目录中时,在设置_wp_page_template时,应将该子目录与模板名称一起包含。

因此,如果您的page-bottom-sidebar.php位于主题的page-templates目录中,而不是:

'Services Content'=>'page-bottom-sidebar.php'

你需要:

'Services Content'=>'page-templates/page-bottom-sidebar.php'