Can I make Wordpress use a custom template for a child page
-
22-04-2021 - |
Question
I have a custom post type called communities. Then I have the appropriate single-community.php page. Works great!
So we want to use some of these "communities" as parent pages. Then underneath them we'd have multiple child pages.
Can I have the parent pages use single-community.php and the child pages use single.php or something similar?
La solution
You can filter template_include
and replace the single-community.php
with a single-child-community.php
.
Example
add_filter( 'template_include', function( $template ) {
if ( ! is_singular() )
return $template; // not single
if ( 'communities' !== get_post_type() )
return $template; // wrong post type
if ( 0 === get_post()->post_parent )
return $template; // not a child
return locate_template( 'single-child-community.php' );
});
Licencié sous: CC-BY-SA avec attribution
Non affilié à wordpress.stackexchange