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

Tyler Armstrong
Tyler Armstrong
13,887 Points

Single Page WP Theme

Hey everyone! So I'm building a WP One Page Theme and I want each page to display on my static front-page. I would like the user to be able to add/edit content of the page in the Dashboard versus the markup. When I view the page template the_content() is working as advertised, but when I require the page template on my static front-page, everything else shows up normally but the_content() is blank. Any ideas?? Thanks!

work.php file

  //  Work Template Page -- the_content() works perfectly 


<!-- works -->
<section id="works">

<!-- container -->
<div class="container">
    <!-- row -->
    <div class="row">

        <!-- head -->
        <div class="head big wow bounceIn">
            <h3>Our
                <span class="blue">Works</span>
            </h3>
            <div class="head-break-line">
                <span class="head-line-blue"></span>
            </div>

            <?php if ( have_posts() ) : while( have_posts() ) : the_post(); ?>
            <h6 class="subtext"><?php the_content(); ?></h6>
            <?php endwhile; endif; ?>

        </div>

front-page.php file

//  Static Front-Page  --  every thing works, but the_content() is blank on this page

<?php require('work.php'); ?>

1 Answer

Tyler Armstrong
Tyler Armstrong
13,887 Points

Bear with me...I figured out an easy way to have a wp one page theme. Not sure if it's the best practice, but it's what I was going for and it's extremely easy to understand. By the way, I'm converting a html site to a theme...forgot to mention that before.

So here goes...I've required each page individually on my front-page instead of the looping through each page. I did this so I can place each page where I want it, and customize my nav menu:

<?php require('services.php'); ?>
<?php require('work.php'); ?>
<?php require('pricing.php'); ?>
<?php require('team.php'); ?>
<?php require('blog-page.php'); ?>
<?php require('contact.php'); ?>

Then, inside each page (services.php, work.php, etc) I have used WP_Query:

<?php $your_query = new WP_Query( 'pagename=work' ); ?>
<?php while ( $your_query->have_posts() ) : $your_query->the_post(); ?>

  <?php 
     //  I place all of my static page content below this line
     //  This allows me to use the_title() and the_content() functions  ?>

     <!-- head -->
     <div class="head big wow bounceIn">
        <h3>Our
            <span class="blue"><?php the_title(); ?></span>
        </h3>
        <div class="head-break-line">
           <span class="head-line-blue"></span>
        </div>
           <h6 class="subtext"><?php the_content(); ?></h6>
      </div>
      <!-- .head -->

<?php 
    //  more static content here
?>

I then reset the postdata and add a conditional statement to display the footer if somehow someone ends up viewing the page individually:

<?php endwhile; 
  // reset post data (important!)
  wp_reset_postdata(); ?>

<?php
    if ( is_page( 'work' )  ) {    
        // the page is "work" then, 
        get_footer();
    } else { 
        // don't get footer            
    }   
?>   

With this method I've used, I now have a wp one page theme and the user can update their page content from the admin screen without botching the rest of the page.