phpwordpresswordpress-themingcustom-wordpress-pages

Create a page with child pages and have each child have it's own page template


I have some code that creates pages with child pages in WordPress. What i would like to do is have each child have its own page template. As you see below, i am creating child pages of Script and Assets. Currently the child page is created with one page_Full.php added to everything but i would like to have Script and Assets have their own page template. Ideas would be helpful. Thanks in advance.

function CreatePage(){

    $post_title = $_POST['ProjectName'];
    $post_excerpt = $_POST['ProjectDiscript'];
    $tags = $_POST['TypeOption'];

    $pages = array(
        array(
            'name'  => $post_title,
            'title' => $post_title,
            'child' => array(
                'script' => $post_title.'_Script',
                'assets' => $post_title.'_Assets'
            )
        )

    );

    $template = array(
        'post_type'   => 'page',
        'post_status' => 'publish',
        'post_author' => $user_id,
        'post_excerpt'  => $post_excerpt,
        'tags_input'  => array($tags),
    );

    foreach( $pages as $page ) {
        $exists = get_page_by_title( $page['title'] );
        $my_page = array(
            'post_name'         => $page['name'],
            'post_title'        => $page['title'],
            'page_template'     => 'page_Full.php'

        );
        $my_page = array_merge( $my_page, $template );

        $id = ( $exists ? $exists->ID : wp_insert_post( $my_page ) );

        if( isset( $page['child'] ) ) {
            foreach( $page['child'] as $key => $value ) {
                $child_id = get_page_by_title( $value );
                $child_page = array(
                    'post_name'         => $key,
                    'post_title'        => $value,
                    'post_parent'       => $id,
                    'page_template'     => 'page_Full.php'
                );
                $child_page = array_merge( $child_page, $template );
                if( !isset( $child_id ) ) wp_insert_post( $child_page );
            }
        }
    }
    wp_die();

}

add_action( 'wp_ajax_CreatePage','CreatePage' );

Solution

  • Had the idea to create an array for every child page - and it seams to work. Perhaps there is still a better way but for the time being - This does the trick!

     $pages = array(
        array(
            'name'  => $post_title,
            'title' => $post_title,
            'child' => array(
                    'script' => array(
                                    'Pname'      => $post_title.'_Script',
                                    'Ptemplate'  => 'page_Full_Script.php'
                                    ),
                    'assets' => array(
                                    'Pname'     => $post_title.'_Assets',
                                    'Ptemplate' => 'page_Full_Assets.php'
                    ),
                    'settings' => array(
                        'Pname'     => $post_title.'_Settings',
                        'Ptemplate' => 'page_Full_Settings.php'
                     )
            )
        )
    
    );
    

    Here is the child with sub arrays.

    if( isset( $page['child'] ) ) {
            foreach( $page['child'] as $key[] => $value ) {
                $child_id = get_page_by_title( $value );
                $child_page = array(
                    'post_name'         => $value['Pname'],
                    'post_title'        => $value['Pname'],
                    'post_parent'       => $id,
                    'page_template'     => $value['Ptemplate'],
                );
                $child_page = array_merge( $child_page, $template );
                if( !isset( $child_id ) ) wp_insert_post( $child_page );
            }
        }
    

    And here is where the child array is set as var to be iterated through so that the info can be inserted into the child_page var to be merged and the post inserted. Thanks to any who took the time to review. Cheers