I know how to create pages automatically when a theme is activated, but I need help to figure out how to also programmatically create child pages at the same time.
For example:
- Page 1
- Page 1.1
- Page 1.2
- Page 2
- Page 2.1
- Page 2.2
- Page 2.3
- Page 3
- Page 3.1
- Page 4
- Page 4.1
- Page 4.2
And here is my function for automatic page creation (top level only).
if (isset($_GET['activated']) && is_admin()){
add_action('init', 'create_initial_pages');
}
function create_initial_pages() {
$pages = array(
'page1' => 'Page 1',
'page2' => 'Page 2',
'page3' => 'Page 3',
'page4' => 'Page 4'
);
foreach($pages as $key => $value) {
$id = get_page_by_title($value);
$page = array(
'post_type' => 'page',
'post_title' => $value,
'post_name' => $key,
'post_status' => 'publish',
'post_author' => 1,
'post_parent' => ''
);
if (!isset($id)) wp_insert_post($page);
};
}
Does anyone know how to extend this function to also create child pages (if they don’t already exist) when the theme is activated?
As @Soulseekah said, you can do this with post_parent. I didn’t test with the following code, but it should work
The example by @Tareq was very helpful, but instead of creating multiple child pages for the parent page, it would make each child page a sub-parent page.
Here is the fixed/improved function (I’m sure that this can be improved even more as the page checks are not that thorough):