[wp-hackers] Children template?
Simon Blackbourn
piemanek at gmail.com
Tue Feb 1 11:45:46 UTC 2011
>
> Currently... we can create "page-slug.php" or "page-id.php" to apply a
> template to a page automatically. How can I do...
> "page-slug-children.php" or "page-id-children.php" to auto-apply the
> template to all children?
>
>
I had a similar situation to you, I needed to initially apply a template to
all existing child pages, then I also wanted to override the template chosen
on the page edit screen to ensure the correct one is always applied.
To apply a template to all children of a particular page, run the following
code just once then remove it:
$pages = get_posts( array(
'post_type' => 'page',
'post_parent' => 49
));
if ( $pages ) {
foreach ( $pages as $page ) {
update_post_meta( $page->ID, '_wp_page_template',
'page-my-template.php' );
}
}
And to auto-apply a particular template to certain child pages:
add_action( 'save_post', 'apply_my_template', 10, 2 );
function apply_my_template( $postID, $post ) {
if (
!current_user_can( 'edit_post', $postID )
|| 'page' != $post->post_type
|| wp_is_post_revision( $postID )
|| wp_is_post_autosave( $postID )
|| 'auto-draft' == $post->post_status
|| 'trash' == $post->post_status
)
return $postID;
if ( 49 == $post->post_parent )
update_post_meta( $postID, '_wp_page_template',
'page-my-template.php' );
}
HTH
Simon
More information about the wp-hackers
mailing list