WordPress.org

Ready to get started?Download WordPress

Forums

Make multiple pages and set template/content on theme activation? (9 posts)

  1. chaos67731
    Member
    Posted 1 year ago #

    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

  2. chaos67731
    Member
    Posted 1 year ago #

    Anyone?

  3. chaos67731
    Member
    Posted 1 year ago #

    Anyone?

  4. catacaustic
    Member
    Posted 1 year ago #

    If you've got that code already, just wrap it in a for() loop for 6 iterations and it will add in 6 pages. I would assume that you have different titles and content for each page, so I'd do it something like this (not tested at all...):

    $pages = array (
        array (
            'title' => 'Page 1 Title',
            'content' => "Page 1 content."
        ),
        ....
    
        array (
            'title' => 'Page 6 Title',
            'content' => "Page 6 content."
        )
    );
    
    foreach ($pages as $page) {
        $id = w_insert_post(
            'post_title' => $page ['title'],
            'post_content' => $page ['content'],
            ...
        );
        .... // Add the rest in here.
    }
  5. chaos67731
    Member
    Posted 1 year ago #

    I keep looking at the code but I cant seem to make heads of what I should do

  6. chaos67731
    Member
    Posted 1 year ago #

    This is as far as I have got, it will make the pages but not set content or a page template per page.

    if (isset($_GET['activated']) && is_admin()){
        add_action('init', 'create_initial_pages');
    }
    
    function create_initial_pages() {
    
    $pages = array(
    	'Services' => array(
    		'Services Content'=>'page-wide.php'),
    
    	'Prices' => array(
    		'Prices Content'=>'page-wide.php'),
    
    	'F A Q' => array(
    		'FAQ Content'=>'page-wide.php')
    );
    
    foreach($pages as $page_url_title => $page_meta) {
            $id = get_page_by_title($page_url_title);
            $page = array(
                'post_type'   => 'page',
                'post_title'  => $page_url_title,
                'post_name'   => $page_url_title,
                'post_status' => 'publish',
                'post_author' => 1,
                'post_parent' => ''
            );
            if (!isset($id)) wp_insert_post($page);
    
    	$page_check = get_page_by_title($page_url_title);
    
    	foreach ($page_meta as $page_content => $page_template){
    
    	if(!isset($page_check->ID)){
    		$page_url_title = wp_insert_post($page);
    		if(!empty($page_template)){
    		update_post_meta($page_url_title, '_wp_page_template', $page_template);
    	}}
    
    	/// End of Second foreach
    	}
    	// End of first foreach
        };
    }
  7. catacaustic
    Member
    Posted 1 year ago #

    That's messed it up a fair bit... :/

    Firstly, you're not setting page content because you don't have a post_content value in your wp_insert_post() call. You need to set the content there so that you actually have content in the post. But looking at your page array, you don't actually ever have any content to put into the page anyway. Look at my example again. There's two array keys called title and content. If you need more info on how PHP uses arrays, just check the manual. While what you're doing will work, it's really not the best way of doing things and will cause a lot of confusion when you go back and look at it again.

    As for why it's not adding the template, I'm not sure. While the code looks like it should, that never means much in real life. You'll need to do some basic de-bugging to find out just what's happening there. In a case like this, it should as easy as adding in some echo ""; statements with the values that you think are there, and what you're expecting to be returned. That way you'll be able ot see exactly what the code is using and what it's returning.

  8. chaos67731
    Member
    Posted 1 year ago #

    I have got the code working now, here it is in case you or anyone else could use it. . . and feel free to see if there is anything I could do better on it.

    if (isset($_GET['activated']) && is_admin()){
        add_action('init', 'create_initial_pages');
    }
    function create_initial_pages() {
    
    $pages = array(
         // Page Title and URL (a blank space will end up becomeing a dash "-")
        'Services' => array(
            // Page Content     // Template to use (if left blank the default template will be used)
            'Services Content'=>'page-bottom-sidebar.php'),
    
        'Prices' => array(
            'Prices Content'=>'page-wide.php'),
    
        'F A Q' => array(
            'FAQ Content'=>' '),
    );
    
    foreach($pages as $page_url_title => $page_meta) {
            $id = get_page_by_title($page_url_title);
    
        foreach ($page_meta as $page_content=>$page_template){
        $page = array(
            'post_type'   => 'page',
            'post_title'  => $page_url_title,
            'post_name'   => $page_url_title,
            'post_status' => 'publish',
            'post_content' => $page_content,
            'post_author' => 1,
            'post_parent' => ''
        );
    
        if(!isset($id->ID)){
            $new_page_id = wp_insert_post($page);
            if(!empty($page_template)){
                    update_post_meta($new_page_id, '_wp_page_template', $page_template);
            }
        }
     }
    };
    }
  9. itknol
    Member
    Posted 8 months ago #

    Thank you, chaos67731. That saved me a lot of headbanging.

  10. -wp-user-
    Member
    Posted 8 months ago #

    Thank you for including the finished code.
    Where are you inserting this, functions.php?
    Is it inserted between <?php and ?> or use the wp_insert_post() call?

Topic Closed

This topic has been closed to new replies.

About this Topic