0

This might be a shoot in the dark but here goes...

I know that starting with wp 3.4 i can put page template aka page-template.php in sub folders. (ref: Page templates in subdirectories) which is great and helps me to make a lot of order in an over-crowded custom theme folder.

this works - however:
i need to auto-create many pages on theme activation and thus, i use the script i added below.

but:
picking / setting the page template doesnt work if the page is in a sub folder.

The script i use:

$start_number   = 2;
$iim_pages      = array('tools', 'clients-manager', 'task-manager');

foreach($iim_pages as $page) {

    $page_title     =   ucwords(str_replace('-', ' ', $page));
    $page_filename  =   'page-'.$page.'.php';

    $page_check = get_page_by_title($page_title);
    $new_page = array(
        'post_type'     => 'page',
        'menu_order'    => $start_number,       
        'post_title'    => $page_title,
        'post_content'  => '',
        'post_status'   => 'publish',
        'post_author'   => 1,
    );
    if(!isset($page_check->ID)){
        $new_page_id = wp_insert_post($new_page);

        update_post_meta($new_page_id, '_wp_page_template', $page_filename);  
        // THIS WORKS! : add_post_meta($new_page_id, 'testmeta', $page_filename);  

    }

    $start_number++;
}

I tried many variation but it seems that wp doesnt set the page template
unless in outside the sub-folder

can anyone offer a solution?

  • Why do you need a page template for every single page? It sounds like a lot, could you not consolidate some of them using post meta? A theme shouldn't really be creating pages, what happens if the theme is deactivated and activated multiple times? – Tom J Nowell Aug 12 '14 at 21:37
  • that wont happen... this is a custom theme acting as a custom software. each page would display a custom post type data. this is what i need so ;) ... – Sagive SEO Aug 12 '14 at 21:39
  • Could you not just reuse the post type archives then? – Tom J Nowell Aug 12 '14 at 21:40
1

Instead of having a page template for all of them, have a single page template and use get_template_part to pull in the parts unique to each page.

So instead of:

update_post_meta($new_page_id, '_wp_page_template', $page_filename);

You'd have:

update_post_meta( $new_page_id, '_wp_page_template', 'newpagetemplate.php' );
update_post_meta( $new_page_id, 'sagive_page_type', $page );

Then inside your new page template:

$type = get_post_meta( get_the_id(), 'sagive_page_type', true );
get_template_part( 'parts/type', $type );

Giving you a parts folder with the files type.php, type-tools.php etc etc

But even more reliable, you suggested these page templates are for showing custom post types. If this is true then you could instead make use of archive.php to do this, and use the custom post type archives. This would simplify your code and allow you to use archive-tools.php etc, and removing the need to create any pages to begin with.

| improve this answer | |
  • this sounds great (and saves a lot of coding) - trying... brb – Sagive SEO Aug 12 '14 at 22:01
  • 1
    this is a much smarter solution i should have tought of this on my own - thanks a lot mate ;) – Sagive SEO Aug 12 '14 at 22:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.