WordPress Development: The Loop
Posted on July 4th, 2009
A vital part of a WordPress theme is commonly know as the loop. The loop is a while loop that loops through the available post for the requested page and displays them. Although it may seem counterintuitive, the loop is also used for single blog posts and pages. This is simply to have one function which can handle all the different types of media.
The loop is made up of a couple of different functions. The function that the loop is actually based upon is have_posts(). Which sets up the way for another function, the_post(), which will set up for other functions to output the current content to the browser. The “output functions” include the_permalink(), the_title(), the_time(), the_author(), the_content(), the_tags() and the_category(), which are fairly self-explanatory. Others also exist, but these are the main functions. Let’s take a look a basic loop (edited for simplicity):
[code language="php"]if (have_posts()) {
while (have_posts()) {
the_post();
// Post output here
}
else {
// Nothing found
}[/code]
As you can see, we first check if have_posts() is true (if there’s no posts to display, it will return false). We then use a while loop with have_posts() as the expression. Inside the loop, we run the_post(), which basically sets up a reference for the output functions to work off of.
There you have it, a basic version of the loop. If you want more complete examples of the loop in action, I would recommend checking out the default theme of WordPress. Specifically, the index.php, single.php and page.php files.
Posted in Development | | | Digg This | del.icio.us | Technorati
Related Topics:
Web Development: Javascript While Loops
Web Development: Javascript For Loops
WordPress Development: Theme Basics
Everything PHP: For Loops
WordPress Development: IRC Development Chats
Comments
Sorry, the comment form is closed at this time.
No comments yet.