Information Technology Reference
In-Depth Information
IV
page. That honor belongs to what is commonly referred to in WordPress circles as simply “The
Loop.”
If you don't have a blog without an Index page, you don't have a WordPress blog without The Loop.
This is a PHP function that displays each post in reverse-chronological order on your Index page. It
incorporates the styles specified in the style.css file associated with your theme, and puts
everything in that you want.
As we discussed earlier, loops are functions that basically revolve around a series of dilemmas we
ask the computer to solve for us. A loop asks the question “what if” and behaves differently
depending on the answer(s) it gets. If something is a pineapple, then we want some ham. If it's not
a pineapple, but it's not a pine tree, we want to do something else. If this thing is just an apple,
sometimes we want a pie, other times a telephone.
PHP loops also ask the question “what if?” over a period of time.
While something exists, we should do that. If it disappears, we
should do something else. Most important, it stops asking the
question at some point, lest the loop become infinite—a very bad
thing.
What WordPress is most concerned within its loop is whether
there's a post available. So the basic question it asks before initi-
ating The Loop is another simple one:
caution
An infinite loop occurs when
there is no way for a program to
stop, such as when you haven't
described what to do in each
“what if?” situation. The result of
an infinite loop is that the pro-
gram eats up all CPU time and
could cause the computer to
freeze up. Avoid infinite loops at
all costs when writing your own
scripts.
<?php if (have_posts()) : ?>
Roughly translated, this means: “Are there posts in this blog?”
More to the point, the page wants to know if there's any content
in the WordPress database. If the answer is yes, The Loop is
turned on:
<?php while (have_posts()) : the_post(); ?>
This code says “Oh joy, there's a post in the database! Please, database, put the contents of this
post on this page here!”
Once it has its post retrieved from the database, many wonderful things might occur (a place for a
thumbnail image, some background color, links to other posts, most anything really) depending on
your installed theme or plug-ins. But one thing is guaranteed—The Loop will end, in this fashion:
<?php endwhile; ?>
Then the function itself will end, in this fashion:
<php endif; ?>
Search WWH ::




Custom Search