Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


Begginers Wordpress question?

Hi there, I am on the creating wordpress theme tutorial.

I am having a little difficulty understanding this line of code:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

I am not understanding the significance of the

: while ( have_posts() ) : the_post(); 

part of the code. Any help explaining that to me would be much appreciated :)

2 Answers

37,033 Points

How it breaks down is the if statement checks to see if there have posts and the while acts as a foreach loop. So it's kind of like putting if ( $posts > 0){ foreach ($posts as $post){ // Lines of code to put out for each post }; else { echo "Nothing is found page or text" } } in php. Hope that helps clarify what you're looking at.

Thank you that helps :) I know html and css pretty fluently and the one thing that I am not liking about php at all is how there are so many different ways to communicate the same thing. Anyways, thanks for the help!

37,033 Points

It's just a special Wordpress thing. And don't forget the_post(); because its like a key to let you access the post fields like title.