Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress One Page WordPress Site

Jerome Diehl
Jerome Diehl
2,678 Points

How to pull in different templates for each page

I completed the course and I wish to have each page bring their own separate template structure. For instance, I want the contact section at the bottom to have three columns. Unfortunately the content-page.php file loops the same structure over each 'section' and just fills in the_title and the_content of each page. What is really happening with this one page site it is grabbing information from different pages and jamming them into the same template. I want to make each section its own template that can be modified separately. Any idea how to do this?

Gregor Ojstersek
Gregor Ojstersek
17,618 Points

Hey Jerome,

you can make an if statement to check for the sections name and set the template for the specific section. You have to declare the global $post variable and than put the conditional inside the WP_Query while loop like this:

<?php 
    $args = array(
        'post_type' => 'page',
        'order' => 'ASC'
    );
    $the_query = new WP_Query( $args );         
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?> 
    <?php $section_name = $post->post_name; 
    if ( $section_name == 'contact' ) :
        get_template_part( 'content', 'contact' ); else: 
        get_template_part( 'content', 'page' ); ?>

<?php endif; endwhile; endif; ?>