Quantcast
Channel: WordPress › Support » Forum: Hacks - Recent Topics
Viewing all articles
Browse latest Browse all 8245

chaos67731 on "Make multiple pages and set template/content on theme activation?"

$
0
0

OK, I have two different code and I need them mixed together more or less.

I want to be able to make 5 or 6 pages on a site when my theme is activated.

When it makes the pages, I want it to set the template that that page will use and set content to that page.

Here is code I have that will just 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);
    };
}

Here is code that will make only one page but set the content and template that page should have.

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);
                }
        }
}

Thanks


Viewing all articles
Browse latest Browse all 8245

Trending Articles