Thord Daniel Hedengren - Smashing WordPress_ Beyond the Blog-Wiley (2014)

(avery) #1

CHAPTER 3 • The Loop 43


Naturally, you’d want a more comprehensive error message than the one in this code, but
that’s not the point right now.


The basic loop checks whether there are any posts to return, and in turn the loop is controlled
by the global blog settings (how many posts to display and such) and your whereabouts on the
blog. A single post would return just one post (the one you want, presumably), whereas a
category listing would return the number of posts specified in the WordPress settings, but
only the ones that belong to that particular category.


If there are posts, a while loop is started, and as long as there are posts to return, as con-
trolled by the situation and settings, posts will be returned and displayed. When the while
loop is done (all posts that should be returned have been output), it ends with endwhile,
and then the loop ends with endif.


Should there be no posts that match the criteria of the particular situation, the else clause is
called, and that’s when the error message (or similar) will be output (or nothing at all, if there
is nothing defined). After that, the loop ends.


So the loop actually loops content from the database, based on the WordPress settings and
any criteria that the page you’re on may bring. Makes sense, doesn’t it?


SEPARATING THE LOOP USING THE TEMPLATE PARTS


There was a time when the loop and its output would always be in the theme’s index.php
template file, and possibly several other template files as well. These days, not all themes
include the entire loop in index.php. By using the get_template_part() include tag
(mentioned in Chapter 2, “The WordPress Syntax”) along with a separate loop template file,
you can separate the loop from your various templates, reusing it when appropriate. In fact,
whereas some template files will contain the loop itself and not rely on gettemplate
part() for loop inclusion, others will use loop.php but render the content differently
depending on what part of the site is being displayed. This method is still fairly common, and
although there is nothing wrong with breaking out the entire loop into its own template
file(s), these days, themes tend to be a bit fine-grained.


The basic premise is simple enough. If you break out the real output of the loop, which would
be the actual content, rather than the whole loop, it will be easier to reuse the code. So instead
of having a default loop.php template and then having a loop-category.php template as you
would before, using both the catch-all loop and the loop for category archives, you’d stick to
just the content output. This means that you’ll have a content.php template with catch-all code
should there not be a dedicated loop output file to include, and you’ll use content-archive.php
for the aforementioned category archive. Why not content-category.php, you might ask? You
could use that, of course, but chances are most archive pages will have the same sort of
content listing; thus you can reuse content-archive.php in different types of archives.


Let’s look more specifically at a category archive as an example. Category archives often look
different from single posts, but multiple ones could be generated by the same loop, although
the loop contents would differ. By using get_template_part() to include the actual

Free download pdf