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

(avery) #1

CHAPTER 4 • WordPress Theme Essentials 85


// Nothing in the loop?
else :
?>




Nothing Found



We're sorry, but we couldn't find anything for you. Please try and
search for whatever it was you were looking for.








There are two minor additions to this code, not mentioned previously. First, there’s the
the_date() heading, which will only output the day’s date once per instance, so if you have
two posts on the same date, you will see it only once. This line is found directly after the loop:


// Show the date once per page
the_date( '', '

', '

' );


Then there’s the conditional tag that checks whether this is a singular page or post and outputs
comments_template() if it is, like this:


// Load comments if singular
if ( is_singular() ) {
comments_template( '', true );
}


At this stage, you actually have an almost-working theme. There are some elements that need
to be declared in functions.php, which you’ll do in a bit, but before you wrap this up, you
should do this right and add some more template files.


BREAKING OUT THE LOOP


Although it isn’t mandatory, I find it useful to break out the loop from the template files.
Either that or the output of the loop, depending on if you want the whole thing in a separate
file or just the content part. In this case, just break out the content part. Look at the earlier
code block; what you want to do is to move the article block to a file of its own. Because
this is the default post look and feel, you’ll just call it content.php. With the article block
in content.php, you’ll replace it in index.php with this:


get_template_part( 'content', get_post_format() );


This looks for content-X.php to include, where X is the post format, fetched with getpost
format(). If the post in question is a regular post, it’ll just default to content.php, but if it is

Free download pdf