From f3dbdda635541d95bd085383869698a9918163ca Mon Sep 17 00:00:00 2001 From: morishita <40419910+xaxaxixd@users.noreply.github.com> Date: Thu, 11 Jan 2024 08:21:46 +0900 Subject: [PATCH] fix: templates/wordpress/functions --- templates/wordpress/theme/functions.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/templates/wordpress/theme/functions.php b/templates/wordpress/theme/functions.php index 907ddf9..2d58457 100644 --- a/templates/wordpress/theme/functions.php +++ b/templates/wordpress/theme/functions.php @@ -127,3 +127,23 @@ function filter_ptags_on_images($content){ function getIdBySlug($page_slug) { return get_page_by_path($page_slug)->ID; } + +add_filter( + 'page_template', + function ($template) { + global $post; + if ($post->post_parent) { + $parent = get_post( + reset(array_reverse(get_post_ancestors($post->ID))) + ); + $child_template = locate_template( + [ + 'page-'.$parent->post_name.'-'.$post->post_name.'.php', + 'page-'.$parent->post_name.'-page.php', + ] + ); + if ($child_template) return $child_template; + } + return $template; + } +);