Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Page and subpage templates WordPress

  • SOLVED

I have a page configured and multiple sub-pages underneath.
I have configured a custom template into this page and I want that all of his sub-pages to use the same one, instead of going into each one and configure this same template.

The idea is if in the future I add another sub-page to this page, it automatically shows with the parent page template.

Answers (4)

2010-11-26

Jermaine Oppong answers:

You may need to bump up the price. Its requires work which most of us wont be willing to do for 4 dollars.


Jermaine Oppong comments:

Ok I got a solution but would be respectful if you supported me with a bit more for the work. Try this in your functions.php


function switch_page_template(){
global $post;

$current_page_template = get_post_meta($post->ID,'_wp_page_template',true);
$parent_page_template = get_post_meta($post->post_parent,'_wp_page_template',true);

$parents = get_post_ancestors($post->ID);

if($parents){update_post_meta($post->ID,'_wp_page_template',$parent_page_template,$current_page_template);}

}

add_action('save_post','switch_page_template');



The should should apply once you save your page. You can use this to switch it earlier


Jermaine Oppong comments:

Try the first piece of code. IF the first piece of code dont work then try this instead. This is just an alternative.


function switch_page_template(){

global $post;

$current_page_template = get_post_meta($post->ID,'_wp_page_template',true);

$parent_page_template = get_post_meta($post->post_parent,'_wp_page_template',true);

$parents = ($post->post_parent==$post->ID) ? return true : return false;

if($parents){update_post_meta($post->ID,'_wp_page_template',$parent_page_template,$current_page_template);}

}

add_action('save_post','switch_page_template');


The first block of code and this should work after you set the parent and save the post. Or when you view it in your browser :)

2010-11-26

Chris Lee answers:

From my knowledge there is no way to trigger and force child page templates of a particular parent.

The easiest way to do this is to trigger a template from when you post a page/post: http://codex.wordpress.org/Pages#Creating_Your_Own_Page_Templates

You go the more difficult route and try to accomplish something with get_query_template() http://core.trac.wordpress.org/browser/tags/3.0.1/wp-includes/theme.php [Line 713] .


2010-11-26

Nilesh shiragave answers:

FIRST MAKE SURE TO BACKUP YOUR FILES.

There is no way to create it. I have one trick to create it by creating two more page templates.

Steps
1] First rename your themes default page template file i.e. "page.php" to a different name like "page_default.php"

2] Now create new file "page.php" and add following code inside that


<?php
if($post->post_parent == '2')
{
include(TEMPLATEPATH.'/your_page_template.php');
}
else
{
include(TEMPLATEPATH.'/page_default.php');
}
?>

In above code replace 2 with the ID of the parent page. I am assuming you have just one parent page.

and "your_page_template.php" with the file name of you page template.

Let me know if you find any difficulties.

2010-11-26

Andrzej answers:

+1 for Jermaine