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 trialJosh King
Courses Plus Student 43 PointsOne-page Wordpress with conditional tags for different layouts?
I'm working through building a one-page Wordpress theme, and some of the way, followed the tutorial here on Treehouse.
The trouble I'm having is, while it works great from the tutorial, I'm trying to pass some conditional statements through and it doesn't seem to be working out for me.
I've tried it a couple different ways:
- Running a conditional statement from the 'content-page.php' page
This seems to be a less flexible way to do it. While it keeps everything in one file, it seems to be a bit cumbersome.
- Running the conditional statements on the one-page-site.php file
What I have going is I have the WP_Query statements, then the loop, then a series of conditional statements outputting different files for the templates that I want to use.
I'm trying to set a different template for the 'home' page (the first page in the one-page site) and a different template for the 'blog' page (which will display an abridged arrangement of the latest posts, with a link to the full blog).
I'm thinking I'm either doing it all wrong, or maybe there is something with the way I set it up (i.e. since the 'home' page is set to the 'one-page-site.php' template), or it has something to do with the loop (wrong code?).
Here is the way I've currently got it set up:
<?php
/**
* Template Name: One Page Site
*/
get_header();
$args = array(
'post_type' => 'page',
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
if ( is_front_page() ) {
get_template_part( 'content', 'home' );
}
elseif ( is_page('blog') ) {
get_template_part( 'content', 'blog' );
}
else {
get_template_part( 'content', 'page' );
}
endwhile; endif;
get_footer(); ?>
Basically, 'content-home.php' is an intro page, 'content-blog.php' will display the latest posts, and then 'content-page.php' will render normal page layout for this one-page site.
Right now it just repeats the content of the intro page in every section.
What do you guys think? Am I a complete idiot?
3 Answers
Zac Gordon
Treehouse Guest TeacherIf you have separate pages on the site then you should rely on the default WordPress templates and naming conventions for controlling the different pages. You should not be including conditional statements in a single template and using that to display multiple pages, which it looks like you're doing here.
I would also suggest checking out this article on resetting the Loop: http://digwp.com/2011/09/3-ways-to-reset-the-wordpress-loop/
Matt Campbell
9,767 PointsFrom what you've described, it's not a one page site you need. You in fact are requiring multiple pages and as such, should use the template hierarchy as Zac mentioned, and build the site in a conventional manner with links and a menu.
Josh King
Courses Plus Student 43 PointsFound somewhat of a solution.
All I did was create another WP_Query for just the home page, and excluded the home page from the rest of the loop. Like so:
get_header();
$home_query = new WP_Query( 'pagename=home' );
get_template_part( 'content', 'home' );
$args = array(
'post_type' => 'page',
'order' => 'ASC',
'post__not_in' => array(5)
);
$the_query = new WP_Query( $args );
if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
get_template_part( 'content', 'page' );
endwhile; endif;
get_footer(); ?>
If you have any suggestions on making this code a bit cleaner, feel free to let me know.
Josh King
Courses Plus Student 43 PointsJosh King
Courses Plus Student 43 PointsYeah, you're right, I got rid of it. I did create another query to pull a custom home page template from, and I reset the post data.
Now I'm just working out how to display different sidebars in each section on the one page template.
I'm really just experimenting, seeing what I can do, delving deeper into what I can do with Wordpress.