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

Paul McBride
Paul McBride
13,514 Points

A Single Page Wordpress site - Help!!

The Problem

I am building a website for a client and I'm beginning to struggle. I'm trying to create a single page website that scrolls to the relevant sub-page/section upon clicking the navigation.

I would like to create each section as a page in Wordpress' backend then have a template load them all together when the website is visited.

Each page will use custom content types etc.

I know its possible, I'm just not sure how to do it. Any one have any idea?

2 Answers

Andrew Shook
Andrew Shook
31,709 Points

Paul McBride, If all the content on the home page is being pulled from one content type then it shouldn't be to difficult. First, your header.php will only need to include the head tag and the opening body tag. Since it's just a one page site there is no need to put the nav in the header, and you'll need to use the same custom query to generate both the nav and the content so they might as well be in the same file.

Now your front-page.php should look something like this:

<?php 
        // Normally you would put you menu in the header, But since you'll need
        // to dynamically generate the menu based on the pages not included it in
        // your header.php.
        get_header(); 
?>

<?php 
    $args = array(
        'post_type' => 'CUSTOM_POST_TYPE',
        'posts_per_page' => NUM_OF_POSTS // defaults to 10.
        'orderby' => "" // not sure how you want them order by I guess you'll need an order.
    );
    $query = new WP_Query( $args );
?>
<nav>
        <?php if ( $query->have_posts() ) : ?>
            <ul id="main-menu">
          <!-- the loop -->
          <?php while ( $query->have_posts() ) : $query->the_post(); ?>
                <?php 
                        // This should take the title, make it lower case and replace all spaces with a dash 
                        // Should make hrefs like "#about-us".
                ?>
                <li><a href="#<?php echo strtolower(preg_replace(" ", "-", get_the_title()));?>"><?php the_title(); ?></a></li>
          <?php endwhile; ?>
          <!-- end of the loop -->
            </ul>

        <?php endif; ?>
</nav>

<div id="main-content">
    <?php 
            // this will rewind the custom query back to the beginning so that we
            // can loop through it again to display the page sections.
            $query->rewind_posts();
     ?>
     <?php if ( $query->have_posts() ) : ?>

       <!-- the loop -->
       <?php while ( $query->have_posts() ) : $query->the_post(); ?>
         <section id="<?php echo strtolower(preg_replace(" ", "-", get_the_title()));?>"><?php the_title(); ?>">
            <h2><?php the_title(); ?></h2>
            <?php the_content(); ?>
         </section>
       <?php endwhile; ?>
       <!-- end of the loop -->

       <?php //wp_reset_postdata(); ?>

     <?php else:  ?>

     <?php endif; ?>
</div>
<?php get_footer(); ?>

This will query all you custom post types and store them in the variable query. First it will loop through the post's and user their titles to make an anchor link for the main menu. Then the query will need to be rewound to it can be looped through again. On the second loop, the the title will be used to make id's for each section that match the anchor links generated in the menu.

Finally, you'll need to include the footer and ensure the html and body tags are close. Best of luck.

Paul McBride
Paul McBride
13,514 Points

So this would work if I set the post type to page?

Then on each page, I can use custom content types etc?

Thanks again. =]