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

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

missgeekbunny
missgeekbunny
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!

missgeekbunny
missgeekbunny
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.