WordPress Theme – WordPress Loop
March 08th, 2009 | Author: Sunny
WordPress Loop is the Loop that is used by WordPress to display each of your blog posts. Using the Loop, WordPress processes each of the posts to be displayed on the current page and formats them within the Loop tags.
Basic WordPress Loop structure will look like this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<!– something you want to repeat within the Loop tag –>
<?php endwhile; else: ?>
<p><?php _e(’Sorry, no posts matched your criteria.’); ?></p>
<?php endif; ?>
Let me explain what is going on. First, you need to know that there is a global object called $wp_query, and when WordPress generates your blog pages, it will build and execute a DB query that results in an array of posts, and which will store in the $wp_query object and also return back to the blog header where it is stuffed into the global $post array.
The have_posts() actually calls $wp_query->have_posts() which checks a loop counter to see if there are any pots left in the post array and return a boolean value TRUE if there is post or FALSE when we have exhausted the loop. And the_post() calls $wp_query->the post() which advance the loop counter and sets up the global $post variable as well as all of the global post data. On the other hand, It takes the current item in the collection of the posts and makes it available for use inside the iteration of the Loop.
Once you understand how the WordPress loop works, then you can use this loop for displaying your blog posts. So, here are some functions that you probably will put inside the WordPress loop:
the_title() – Display the current post’s title
the_time(format) – Display the date with format
the_author() – Display the author name
the_permalink() – a Permanent Link that point to the current post
the_content() – Display the content of the post
There are more technique you can use and display inside the loop. Here I just post some common functions that you will usually use, but there are more function you can do such as comment for the post. You can find more information here:
http://codex.wordpress.org/The_Loop
http://codex.wordpress.org/The_Loop_in_Action
Thanks for posting, definitely going to subscribe! See you on my reader.