$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Set page template automatically according to parent slug|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Set page template automatically according to parent slug

matteradmin9PV0评论

I've seen several similar topics on this forum but still unable to come to a solution.

I have a parent slug named "en". I named its template as "page-en.php". Every time when I make a child page having "en" as the parent, I want to apply the description on "page-en.php" to it. There are many children.

I've been successful when it's only www.SITE/en/ but when there is a child it's not working out (e.g. www.SITE/en/child), and instead showing a reflection from "page.php", which is not desired.

Here are what I want to avoid:

*Using translation plug-ins
*Differentiating with id as it's pain to have separate templates for each id

These are "page" and not "post".

Any insight is deeply appreciated.

Hiro

I've seen several similar topics on this forum but still unable to come to a solution.

I have a parent slug named "en". I named its template as "page-en.php". Every time when I make a child page having "en" as the parent, I want to apply the description on "page-en.php" to it. There are many children.

I've been successful when it's only www.SITE/en/ but when there is a child it's not working out (e.g. www.SITE/en/child), and instead showing a reflection from "page.php", which is not desired.

Here are what I want to avoid:

*Using translation plug-ins
*Differentiating with id as it's pain to have separate templates for each id

These are "page" and not "post".

Any insight is deeply appreciated.

Hiro

Share Improve this question edited Mar 2, 2019 at 15:26 fuxia 107k39 gold badges255 silver badges461 bronze badges asked Mar 2, 2019 at 12:17 SushimaruSushimaru 31 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

You can override the hierarchy and load a specific page template via the page_template filter.

The fastest way is to identify the parent page by ID:

function wpd_page_template_filter( $template ){
    $page = get_queried_object();
    if( 42 == $page->post_parent ){ // en page has ID of 42
        $template = locate_template( 'page-en.php' );
    }
    return $template;
}
add_filter( 'page_template', 'wpd_page_template_filter' );

or you can also identify it by slug with an extra step:

function wpd_page_template_filter( $template ){
    $page = get_queried_object();
    $parent_slug = get_post_field( 'post_name', $page->post_parent );
    if( 'en' == $parent_slug ){
        $template = locate_template( 'page-en.php' );
    }
    return $template;
}
add_filter( 'page_template', 'wpd_page_template_filter' );

If your page is a grandchild or lower, you can get the topmost parent with the get_ancestors function

Post a comment

comment list (0)

  1. No comments so far