Single Page WordPress Theme – Using page templates

Well I am lost, its late and I have been searching google all night. Heres the problem I am running into. I am trying to create a singe page website using wordpress “pages”. How I have initially setup the theme is there are several page templates for ex (‘page-contact.php, page-gallery.php, page-map.php etc.’). Each of these page templates are customized with different meta-boxes and the corresponding page template calls those metaboxes and creates the page. This part is fine and the individual pages display well, however when I try to query all the pages to one single page, thats where I am having problems.

So far I am able to get all the pages to display but only with one template so then my pages don’t post properly with their custom meta and custom template file. I have attached the code below, I am wondering if there is a way to call a variable into the get_template_part(‘page’, $template_name_of_page) to display the proper template for each page.

<?php get_header(); ?>
<?php           
$args = array (
    'post_type'                => 'page',
    'post_parent'            => '29',
    'orderby'                => 'menu_order',
    'order'                  => 'ASC',
);

$query = new WP_Query( $args );

if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {    
        $query->the_post();    
        get_template_part('page', 'countdown');
    }
} else {

}   
wp_reset_postdata();    

?>
<?php get_footer(); ?>

TEMPLATE HIERARCHY

  1. LANDING PAGE OF WEBSITE – Want to pull all pages and display them with their individual template here

    • LANDING PAGE – page-home.php (this has the above section of code)

      • Portfolio – page-portfolio.php ( Child of Landing Page – has custom
        meta boxes called in template file )
      • map – page-map ( Child of Landing Page )
      • Contact – page-contact ( Child of Landing Page )

2 Answers
2

Well I think I seem to be doing what I was intending too. Thanks @Milo for pointing me in the right direction! As You can see below I used the get_page_template_slug(); and str_replace() to filter through the templates used per page.

Can someone Verify that this is a viable option?

$args = array (
    'post_type'                => 'page',
    'post_parent'            => '29',
    'orderby'                => 'menu_order',
    'order'                  => 'ASC',
);

$query = new WP_Query( $args );


if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {

        $query->the_post();
        $tn = get_page_template_slug( $post_id );
        $word = array("page-", ".php",' ');
        $template = str_replace($word,'',$tn);

        get_template_part('page', $template);
    }
} else {

}

Leave a Comment