You are viewing an old revision of this post, from March 15, 2015 @ 12:39:57. See below for differences between this version and the current revision.

Programmatically create child pages on theme activation

Found on WP Stackexchange How to programmatically create child pages on theme activation. [php]$pages = array( array( 'name' => 'page1', 'title' => 'Page 1', 'child' => array( array( 'name' => 'page11', 'title' => 'Page 1.1' ), array( 'name' => 'page12', 'title' => 'Page 1.2' ) ) ), array( 'name' => 'page2', 'title' => 'Page 2', 'child' => array( array( 'name' => 'page21', 'title' => 'Page 2.1' ), array( 'name' => 'page22', 'title' => 'Page 2.2' ) ) ), array( 'name' => 'page3', 'title' => 'Page 3', 'child' => array( array( 'name' => 'page21', 'title' => 'Page 2.1' ), array( 'name' => 'page22', 'title' => 'Page 2.2' ) ) ), ); $template = array( 'post_type' => 'page', 'post_status' => 'publish', 'post_author' => 1 ); foreach( $pages as $page ) { $exists = get_page_by_title( $page['name'] ); if( !$exists ) { $my_page = array( 'post_name' => $page['name'], 'post_title' => $page['title'] ); $my_page = array_merge( $my_page, $template ); $id = wp_insert_post( $my_page ); //if there is any child page, create them by {$id} as 'post_parent' if( isset( $page['child'] ) ) { foreach( $page['child'] as $child ) { $child_page = array( 'post_name' => $child['name'], 'post_title' => $child['title'], 'post_parent' => $id ); $child_page = array_merge( $child_page, $template ); $id = wp_insert_post( $child_page ); } } } }[/php]

Revisions

  • March 15, 2015 @ 12:39:57 [Current Revision] by PeterLugg
  • March 15, 2015 @ 12:39:57 by PeterLugg

Revision Differences

There are no differences between the March 15, 2015 @ 12:39:57 revision and the current revision. (Maybe only post meta information was changed.)

Tags:

No comments yet.

Leave a Reply