Finding Page Template and Displaying Content

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.

Thanks!

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.

<?php 
  $this_page=get_query_var('page_id');
  $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; ?>

Leave a Comment