0

I have a page called Joinery and when open some new pages where I set that their parent is Joinery (under Page attributes), I would like to set Wordpress to automatically use file page-joinery-subpages.php for displaying them without manually selecting template.

My solution is based on this https://wordpress.stackexchange.com/a/218817/85304 ,

function load_usetemplate(){
    global $post;
    // get top level parent page
    $parent = get_post(reset(array_reverse(get_post_ancestors($post->ID))));

    if($parent->ID === pll__(8)  && !is_page(pll_get_post(8)){
        if ($child_template = locate_template( 'page-joinery-subpages.php')) 
            return $child_template;
    }
    return $template;
}
add_filter( 'page_template', 'load_usetemplate' );
2

Before getting the top level parent page, you could check if the page has parents; also you forget to define $template.

This code should work:

function load_usetemplate( $template ){

    global $post;

    if( $post->post_parent > 0 ) {

      // get top level parent page
      $parent = get_post( reset( array_reverse( get_post_ancestors( $post->ID ) ) ) );

      if( $parent->ID === pll__(8) ){
        if ( $child_template = locate_template( 'page-joinery-subpages.php') ) 
            return $child_template;
      }

    }

    return $template;
}
add_filter( 'page_template', 'load_usetemplate' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.