3

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 )
  • Do you wish to have different layouts on single page wordpress? – Milople Inc Aug 22 '14 at 9:12
  • @IndiesWebs yes exactly, i've updated my comment with current hierarchy. – Derrick Workman Aug 22 '14 at 13:43
  • If each page displays correctly individually, then presumably they each call the header and footer templates. If you displayed those templates on a single page you'd have the repetition of header and footer for each. – Milo Aug 22 '14 at 14:41
  • @Milo Yes that would work however what if some of my pages that I make use the the same page template twice? For Example I have a page template called page-fullwidth.php and I decide to use that template for 2 sections of content (two separate pages). Thats why I am having a difficult time querying the pages. – Derrick Workman Aug 22 '14 at 17:45
  • I'm pointing out how a single template won't work for both purposes. you need to have the content area separated into a separate template. perhaps a helpful hint: the page template is saved in a post's meta data under the key _wp_page_template – Milo Aug 22 '14 at 17:50
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 {

}
0

Well you can make a single template for the entire site and may call different page content in different <div> using this code:

<?php
 $page_id = 47;
 $page_data = get_page( $page_id );
 echo $page_data->post_content ;
 ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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