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 trialjaime maclean
191 Pointscustom wordpress loop
what is a loop? the course hasn't covered this information, how can it be covered in a quiz..? am I supposed to know php before starting this course? I was not given any prerequisites for this course, it just kind of started without any context... cool
3 Answers
Jonathan Grieve
Treehouse Moderator 91,253 PointsYou'll learn more about the WordPress Loop with the WordPress Theme Development course. I believe this course is part of the Learn WordPress track which doesn't go into back end coding of WordPress. :-)
Nathan Ward
7,907 PointsThe loop is used to return posts, the basic syntax can be found here: https://codex.wordpress.org/The_Loop . This article should give you a lot of examples of ways to use the loop to retrieve different data. You can also create a custom loop to retrieve different post types and refine the results further.
A basic loop that retrieves the 4 most recent posts with the posts title, content, date and author will look like this:
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 4
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) {
$loop->the_post(); // returns the post
the_title(); // echos the title
the_content(); // echos the content
the_author(); // echos the author
the_date(); // echo the date
} // end while
} // end if
?>
Sue Dough
35,800 PointsYou don't need any programming experience in PHP to understand what a loop is. Think of it as a roller coaster.
For example lets compare WordPress blog posts to a roller coaster that allows 1 person on at a time. There is 5 people waiting in line for the roller coaster with the loop. Each one of those 5 people will ride the roller coaster and do the loop 1 time. Once a passenger exits the roller coaster the next passenger will get on. In WordPress 5 blog post would be like the 5 people and showing all 5 posts on your blog to the visitor would require each blog post going around the loop 1 time. Once the first blog finishes the loop the next 4 will go one at a time until its done and its showing all 5 posts.