Wordpress - Is there a default template file for child pages / subpages?
There is no specifica template for child pages, but you can do this pretty easily with the get_template_part() function.
First create a file called "content-child.php".
Second create a file called "content.php".
Next, inside of page.php, place this:
if( $post->post_parent !== 0 ) {
get_template_part('content', 'child');
} else {
get_template_part('content');
}
Anything that you want displayed on a child page will be placed inside of content-child.php. Anything you want displayed on non-child pages will be placed in content.php.
It's actually very easy, add follow code to your functions.php
add_filter(
'page_template',
function ($template) {
global $post;
if ($post->post_parent) {
// get top level parent page
$parent = get_post(
reset(array_reverse(get_post_ancestors($post->ID)))
);
// or ...
// when you need closest parent post instead
// $parent = get_post($post->post_parent);
$child_template = locate_template(
[
$parent->post_name . '/page-' . $post->post_name . '.php',
$parent->post_name . '/page-' . $post->ID . '.php',
$parent->post_name . '/page.php',
]
);
if ($child_template) return $child_template;
}
return $template;
}
);
Then you can prepare templates with following patterns:
[parent-page-slug]/page.php
[parent-page-slug]/page-[child-page-slug].php
[parent-page-slug]/page-[child-post-id].php