So, I'm building a theme for a client with one page layout functionality. Currently, I'm allowing the user to create a parent page, and the modules within that page are separate pages that will be set as children of the main page.

Clarifying. I have a home page. Within the home page, I need to have a section to display a product tour and also a map. The client will create the parent page Home and then create a page called Product Tour & Map. These pages will be set as children of Home and I have set up a loop that will pull all children of the page and display them.

Here's where I'm getting stuck. I need to allow my client to create a child page and set a custom page template that will then display on the main page. Does anyone know how I can make the loop work this way, looking for the page template and displaying the content through that page template, based on user choice? I've Googled everything imaginable, searched every forum and I can't find anything helpful, so I figured some of you WordPress mega ninjas could be amazing and give me a little bit of much needed help.

I've found another answer similar to this question here, but I need someone to clarify exactly how I can implement this.

I will be eternally grateful.


  • Why do you want to create a page template for that page? Create a WP_Query, call the specific pages (Product Tour and Map) by their IDs and use any HTML you want.
    – RaajTram
    May 21, 2016 at 22:25
  • 1
    Because I need this to be dynamic and allow my client to select templates and be able to create new pages without having to hard code something new. May 21, 2016 at 23:38
  • I see. I know you've researched everything, but wanted to make sure you've exhausted your options with this plugin too - wordpress.org/plugins/improved-include-page.
    – RaajTram
    May 22, 2016 at 1:07
  • Thanks. Again, this doesn't help as it requires hard coding when you need to add an additional page. May 22, 2016 at 1:20
  • If I understand you correctly, doing a query based on the meta key _wp_page_template (or something similar to that) might be what you want. It will let you pull out the pages that have a particular template assigned. On my phone atm so I can post a proper answer later on if that's what you were after? I'm still a little unclear from your question
    – Tim Malone
    May 22, 2016 at 6:46

2 Answers 2


I was able to figure this out and make it dynamic.

I created a variable $template that I put inside the loop, in which I stored the page template.

$template = get_post_meta( $post->ID, '_wp_page_template', true );

Then, I utilize this where I need the child pages to show up.

<?php include(locate_template($template)); ?>

This is working for me and is pulling each child page into the parent page according to their chosen page template. Here, for your enjoyment is the entirety of the code.

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

  <?php include(locate_template($template)); ?>

<?php endwhile; endif; ?>
  • 1
    I'd say get_template_part would be better than include.
    – cjbj
    May 22, 2016 at 12:18
  • 1
    Can you pass a variable through get_template_part? I tried, but I didn't think you could. May 22, 2016 at 12:20
  • All the research I did recommended using include and locate_template. May 22, 2016 at 12:24
  • 1
    No, you can't do that (passing a variable). I always use a global variable for information that needs to be available in different template parts to avoid the Theme Check plugin nagging about the use of include
    – cjbj
    May 22, 2016 at 12:24

The question you linked to doesn't quite do what you're wanting - it allows you to get the template assigned to a page you're querying, but what you're actually wanting is to get a page assigned to a particular template.

You can do this using the get_pages() function by querying a meta key of the page.

Pages/posts can have a range of fields assigned to them, both built-in fields and custom fields. Anything outside of the standard (such as date, post type, author, etc.) is called 'post meta'. For pages, the 'page template' setting is one such meta item that you can individually query.

Here's how you do it in your case:

$templated_pages = get_pages(
    "meta_key" => "_wp_page_template",
    "meta_value" => "file-name-of-your-template.php",

foreach($templated_pages as $page){
  print_r($page); // print everything so you can see what you've got
  echo get_the_title($page); // print the title
  echo apply_filters("the_content", $page->post_content); // print the content of the page

For more details on the get_pages() function you can see the Wordpress function reference. Depending on what information you want to get out, there are other functions you can look up as well - eg. you can get the title and content as described above, or anything else you need, by using the information present in the $page variable within that foreach loop.

  • Thanks. Do I need to hard code each page template name for this to work or is it dynamic? May 22, 2016 at 7:20
  • You'll need to hard code it - if it's dynamic, how will it determine it? These are custom templates you're setting up, so Wordpress has no way of determining what you want to look for.
    – Tim Malone
    May 22, 2016 at 7:21
  • It seems as though there should be a way to make this dynamically find the page template and use that, shouldn't there? May 22, 2016 at 11:42
  • Hmm I don't think I understand. You want to find pages that you've set to a particular template, but you want Wordpress to guess which template you're looking for? How would it know what template you want?
    – Tim Malone
    May 22, 2016 at 20:22
  • Look at the answer I gave, I was able to figure it out. May 22, 2016 at 20:23

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.