[wp-hackers] Children template?
Bryan Harley
bryanharley at gmail.com
Tue Feb 1 18:02:16 UTC 2011
Thanks Simon!
On Tue, Feb 1, 2011 at 3:45 AM, Simon Blackbourn <piemanek at gmail.com> wrote:
>>
>> 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
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
More information about the wp-hackers
mailing list