_wp_page_template to dynamically use template

I’m wondering if it is at all possible to set display a page template chosen by the user within the context of a query_posts using get_template_part. In other words, I have set up a page template to pull all of it’s child pages and display them, but I need each child page to display according to the page template that is chosen in the page editor. Is there any way to make that happen? Here is the code I have for the parent page template so far.

  query_posts( array('post_type'=>'page', 'posts_per_page' => -1, 'post_parent' => $this_page, 'orderby' => 'menu_order', 'order' => 'ASC') ); 
  if(have_posts()): while(have_posts()): the_post(); 

<?php get_template_part('_wp_page_template'); ?>

<?php endwhile; endif; ?>

So, as you can see, I’ve got the query pulling all of the child pages. I’d like to know how I can make each of these child pages display according to their chosen page template.


Solutions Collecting From Web of "_wp_page_template to dynamically use template"

First things first, never use query_posts, it overwrites the main query and can cause unwanted side-effects. Use WP_Query instead.

_wp_page_template is a post meta key, so the first thing we need to do is to load the value stored in that key for each page, using get_post_meta. That will give us the filename, which we can then try to load.

$this_page = get_queried_object_id();
$child_pages = new WP_Query(
        'posts_per_page' => -1,
        'post_parent' => $this_page,
        'orderby' => 'menu_order',
        'order' => 'ASC'
if( $child_pages->have_posts() ){
    while( $child_pages->have_posts() ){

        // get the filename stored in _wp_page_template
        $template = get_post_meta( get_the_ID(), '_wp_page_template', true );

        // load the file if it exists
        locate_template( $template, true, false );

    // restore the global $post after secondary queries

In order to save the page template (part) from the admin, I would use custom meta field, and save the template_part name in each page/post. Then call the post meta in the template and pass it to get_template_part.

// meta saved in admin is "child_template_type => "child_part_one"
// where the meta_key = "child_template_type"
// and meta_value = "child_part_one"

$part = get_post_meta($this_id, 'child_template_type');

get_template_part($part); // same as get_template_part('child_part_one')

I would recommend, using Advanced Custom Fields.
Create a Field Group with a Select option and manually key in the template_part names. That way you can store the value as the actual part file name, and let the user see a nice name for each one

child_part_one : View One
child_part_two : View Two
  1. Create a Field Group called “Child List Template Parts” (or something 😉 )
  2. Add a new field with Choice>Select as the field type and name it “child_template_part”
  3. Define the options (above) in the “Choices” field
  4. Add the post type rules in the “Location” section
  5. Save the field group and edit your post/page to select the template part

To get the template part using ACF

$part = get_field('child_template_part', $this_id);