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.


  • if you look at all the code a page template carries with it (head section, header, sidebars, footer etc) is that really what you want to call with the middle of a loop? it seems to me that you are just looking for the page title and content with the layout and formatting as it is in the page template. for that you might need to create specific template part files which you call from the loop ...
    – Michael
    Commented May 22, 2016 at 3:44
  • Yes, that's how I have it set up right now and exactly what kind of advice I need. I've set up template parts with Template Names in order to allow them to be chosen in the dropdown and I need to know how to utilize the template that is chosen dynamically. Commented May 22, 2016 at 4:03
  • my comment was actually trying to point out that you cannot use the page templates as they are, because they contain too much excess structures. you can get the slugs of the page template files automatically in your loop by using this code get_page_template_slug( $post->ID ).
    – Michael
    Commented May 22, 2016 at 4:31

2 Answers 2


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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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