I am wanting to make about 6 pages or so on theme activation, and I have found two ways to do it on this site.
One will make as many pages needed and that is all, the second one will make only one page but allows me to pick a page template and content.
I am looking for a kind of mash up of the both, but have not been able to get it to work. I would like to make as many pages as needed and for each page set a template and content if needed.
Here is the code I have that will make more than one page.
function create_initial_pages() {
$pages = array(
'page1' => 'Page 1',
'page2' => 'Page 2',
'page3' => 'Page 3',
'page4' => 'Page 4'
);
foreach($pages as $page_url => $page_title) {
$id = get_page_by_title($page_title);
$page = array(
'post_type' => 'page',
'post_name' => $page_url,
'post_title' => $page_title,
'post_status' => 'publish',
'post_author' => 1,
'post_parent' => ''
);
if (!isset($id)) wp_insert_post($page);
};
}
And here is code that will make one page but set content and a page template
if (isset($_GET['activated']) && is_admin()){
$new_page_title = 'Sitemap';
$new_page_content = ' ';
$new_page_template = 'sitemap.php'; //ex. template-custom.php. Leave blank if you don't want a custom page template.
//don't change the code bellow, unless you know what you're doing
$page_check = get_page_by_title($new_page_title);
$new_page = array(
'post_type' => 'page',
'post_title' => $new_page_title,
'post_content' => $new_page_content,
'post_status' => 'publish',
'post_author' => 1,
);
if(!isset($page_check->ID)){
$new_page_id = wp_insert_post($new_page);
if(!empty($new_page_template)){
update_post_meta($new_page_id, '_wp_page_template', $new_page_template);
}
}
}
Does anyone know a way to get this done?
Edit: If able, I could also see having use for creating a child page as well but not that needed.