The Loop

The main ingredient of a website, are its posts and pages.
You installed wordpress and you started adding some posts and pages.
WordPress stores them in the “wp_posts” table.
[Note : wp_posts is the table WordPress creates by default, but you can override to change the table prefix to something else when you install WordPress]

When you want to display these in your page, “the loop” comes into play.
WordPress is proactive and gets ready to serve the posts even before the theme executes “the loop”.
If there is no special instruction, WordPress pulls the 10 most recent posts from the database.

“The Loop” means, this code is going to iterate through each one of your posts.

Lets take a look at the PHP code in the index.php of the default theme and see what it does.

1. <?php if (have_posts()) : ?>

2. <?php while (have_posts()) : the_post(); ?>

3. <div class=”post” id=”post-<?php the_ID(); ?>”>

4. <h2><a href=”<?php the_permalink() ?>” rel=”bookmark” ><?php the_title(); ?></a></h2>
5. <small><?php the_time(‘F jS, Y’) ?> </small>

6. <div class=”entry”>
<?php the_content(‘Read the rest of this entry »’); ?>
</div>

7. <p class=”postmetadata”>Posted in <?php the_category(‘, ‘) ?> | <?php comments_popup_link(‘No Comments »’, ‘1 Comment »’, ‘% Comments »’); ?></p>
</div>

8. <?php endwhile; ?>

9. <?php else : ?>
<p class=”center”>Sorry, but you are looking for something that isn’t here.</p>
<?php endif; ?>

Some part of the code removed to make it look simpler for understanding.

Lets take a look at the code closely.

the line number 1:
checks if there are any posts that are to be displayed. If there are any posts, then line numbers 2 to 8 get executed, else line number 9 gets executed.

the line number 2:
so we are in line number 2, which means there are some posts that are ready to be displayed on the page.
we have to go through each one of them and display the following information.

  1. the title of the post
  2. the date it was published
  3. the actual post’s content
  4. the category that this post belongs to, and
  5. a link to the comment form along with the number of comments so far.

All of the above is collectively called as “one post”.
Line number 2, loads one post at a time into a set of variables so that it is available to the calling functions like “the_title()”, “the_content()” etc

line number 3:
Opens up a new “div” element and assigns an id and a class to that “div”.
line number 4:
Gets the title of the post, and the permanent link to this post and creates an anchor tag within a “h2” tag.
line number 5:
Gets the time at which the post was published.
line number 6:
Gets the actual content of the post and puts into its own “div” element.
line number 7:
Gets the category that post belongs to and a link to the comment form along with the number of comments so far.
It also closes the “div” tag that we opened on line 3.
line number 8:
Closes the current post as we are done with displaying it, and make the wordpress to go back to line number 2 for the next post.

This loop goes through till we are done with displaying all of the available posts in the posts collection.
line number 9:
When there are no posts to display, we would like the page to specify that. so we just added that line number 9.

Thats all you need to understand from this page.

Additional Notes:
1. the_title() – echoes the title of the current post
2. the_time() – echoes the date and time at which the current post was published
3. the_content() – echoes the current post’s content
4. the_category() – echoes the current post’s category
5. comments_popup_link() – displays a hyperlink that points to the commentform along with the number of comments entered for the current post.

Take a look at the special emphasis on “the current post” on each one of the above lines.

the_post() [seen at line number 2] – makes sure you are dealing with one post at a time, and initializes the variables so that any of these 5 methods can work with the current post

Please use the sidebar for navigating within the Book.