How to create a Wordpress Template or Theme

September 21, 2006

Step Three: the Loop

We will begin with The Loop. The Loop is probably the most important thing you’ll need to understand when doing this “Wordpress thing”. Now, I’ve linked to the official article so that you can read up on the technical aspect of it, and see how powerful this little thing is. But I’ll give you a bare-bones explanation of what it does: it’s what makes your posts do their thing.

This is the basic Loop:


<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

stuff here

<?php endwhile; else: ?>

<?php endif; ?>

In laymans terms, the above says “If there is a post, and during the entire post, then show the post.” What I mean by the part in italics is that the program will “read” the post commands – the meaty part in the middle that tells it what to do. “While” it’s “reading” that part, it displays what it “reads” – the “stuff here” part. Hopefully that makes sense to you! It is, sometimes, the hardest part to grasp.

Then, the “endwhile” says “stop reading the stuff”. Now, at this point, you can just end the entire thing here by removing the “else” portion. But if you want the site to dsplay an error message or something, you can place that here, and *then* “endif”.

And so it continues in a Loop. The program will find a post, read the post, and displays what it reads from beginning to end, then it moves on. If there is another post, it will perform the same function again and again, until there are no more posts for it to find.

Now, the “stuff here” section is what actually defines the post.


<?php the_date('','<h2>','</h2>'); ?>

<div class="post" id="post-<?php the_ID(); ?>">

<h3 class="storytitle">
<a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>

<div class="meta">
<?php _e("Filed under:"); ?> <?php the_category(',') ?> —
<?php the_author() ?> @ <?php the_time() ?> <?php edit_post_link(__('Edit This')); ?>
</div>

<div class="storycontent">

<?php the_content(__('(more...)')); ?>

</div>

<div class="feedback">
<?php comments_popup_link(__('Comments (0)'), __('Comments (1)'), __('Comments (%)')); ?>

</div>

</div> <!--closing .post -->

All of that gobbeldygook tells the loop, basically, to display the contents of your post. That’s the “while” part. So the post will end up looking like this (from a “View Source” standpoint):


<h2>Post Date Here</h2>

<div class="post" id="post-ID#">

<h3 class="storytitle">
<a href="Permanent URL To Post Here" rel="bookmark">Post Title Here</a></h3>

<div class="meta">
Filed under: Category Name Here —
Author's Name Here @ Time Post Was Published     Link to Edit Post Here
</div>

<div class="storycontent">

Post Content Here (with "more" link)

</div>

<div class="feedback">
Link to Comments Popup Window here (# Of Comments Made)

</div>

</div> <!--closing .post -->

Hopefully, that made sense to you, and you’re getting the idea of the Loop. You can do many, many things with the Loop. But we’re trying to keep thing tutorial simple – so we’re gonna stick with the basics for now. But keep in mind, when you want to do some cool things with your site’s content, usually the Loop is where you want to do them!

Pages: 1 2 3 4 5 6 7

View Comments

Sorry, comments are now closed on this post. You may thank the spammers for that one. But if you have any questions, please feel free to email me and ask - maybe it'll make for a good update in a future post. :)