The MU forums have moved to WordPress.org

Assign page templates for default pages (4 posts)

  1. chelgeson
    Member
    Posted 14 years ago #

    Ok here we go...
    I made a plugin to create default pages. So...
    I set up a new static page to be the home page. It has a sidebar area called sidebar-1. And I have assigned default widgets to be activated in this sidebar area by default. This was the user can go to the widget area in their dashboard and see the widgets activated under "sidebar-1" rather than hardcode the widgets and they see nothing.
    I have also created a page called blog. And here is where I am having problems.
    I made a new page template called tempalte-blog.php. I have it calling upon a "sidebar-2" I want the blog page to have a seperate sidebar so that a separate set of default widgets are activated and displayed. I want this blog page to be assigned the template-blog.php file by default.

    I have used both
    'page_template' => 'template-blog.php'
    and
    'post_template' => 'template-blog.php'
    neither are working...the blog page is displaying sidebar-1. I know I have the sidebars registering correctly because when I go into the dashboard->widget menu I see "sidebar-2" and the widgets pertaining to the blog underneath as activated. Also after the blog is created If I go in and create a new page and assign the page template to template-blog. it displays the sidebar-2. It just isnt doing it by default.
    Another weird thing is that if I go to edit the default created blog page and look under page template is is saying the the "default" page template is being used. If I try to change it to "template-blog" nothing happens to the page. in fact it wont change any page template...???

    It is almost as if it was stopped during creation and something was left out of registration....I dont know

    I know this is really long but I could really use someones help! Please if you have any suggestion on how to have a page template assigned for default pages it would be very much appreciated.

    Cody

  2. chelgeson
    Member
    Posted 13 years ago #

    I have done some more digging and this thread seems to be explaining a little more...http://wordpress.org/support/topic/201279?replies=2

    It makes sense that the page template needs to be sent later. Can someone walk me through this code (this is derived from the thread above).

    $my_post_id = wp_insert_post($prop_page);

    if($my_post_id) {
    update_post_meta($my_post_id, '_wp_page_template', 'page-proposal.php');
    }

    how do I implement this?

    Thanks so much!
    Cody

  3. chelgeson
    Member
    Posted 13 years ago #

    Here is the code I have to add the new page.

    `
    function prime_the_blog($blog_id) {
    // add_blog_option( $id, $key, $value ) at http://codex.wordpress.org/WPMU_Functions/add_blog_option
    add_blog_option($blog_id, 'generate_new_page', 'generate_new_page');
    switch_to_blog($blog_id);
    $postdata = array('post_status' => 'publish',
    'post_author' => $user_id,
    'post_date' => $now,
    'post_date_gmt' => $now_gmt,
    'post_modified' => $now,
    'post_modified_gmt' => $now_gmt,
    'post_title' => 'Blog',
    'post_name' => 'blog', /* the slug */
    'page_template' => 'template-blog',
    'post_type' => 'page');
    $newid = wp_insert_post($postdata);
    if ($newid && !is_wp_error($newid)) {
    add_meta($newid);
    } else {
    // your error handling code
    }
    }

    add_action('wpmu_new_blog', 'prime_the_blog');
    `

  4. chelgeson
    Member
    Posted 13 years ago #

    I just wanted to update this thread...I FINALLY figured this out after a really long week.
    I used this code to define my home page

    update_option("show_on_front", "page");
    update_option("page_on_front", $pageid);

    and for some reason it was making my new "blog" default page be the page to display posts and was overriding the page template.

    I used this thread....(notice that I had to revise the code a little bit because I was getting an error)

    http://mu.wordpress.org/forums/topic/12922?replies=16#post-98272

    and because I decreased the priority of the function (see how I added "100" to the end of the add_action line)
    everything now works great!

    So I now have a default static page as my default and a separate blog page to display the blog...each with their own sidebars!!

    thanks
    Cody

About this Topic

  • Started 14 years ago by chelgeson
  • Latest reply from chelgeson