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

(avery) #1

48 PART I • Getting Started with WordPress


The second content template tag available is the_excerpt(). As its name implies, it
outputs only an excerpt of the actual content, by default the first 55 words. All HTML,
whether it is images, paragraph breaks, or YouTube videos, is stripped from this excerpt, so it
should really be used with caution. Also, there are no parameters, which is a bit weird; you
would think that you’d be able to control the length of the actual excerpt, but alas. You can
alter the length by adding a filter to the excerpt_length hook, though, if you want to alter
the number of words used by the_excerpt().

However, the_excerpt() does fulfill a purpose. You know that Excerpt box on the Write
Post/Add New Post screen in WordPress? (See Figure 3-2; depending on your setup you
might have to enable it using the Screen Options settings at the top right, also shown in
Figure 3-2.) If you put something there, the_excerpt() will output that instead of the first
55 words. So if you put a simple “Hi, this is my post!” message in the excerpt field for a post,
the usage of the_excerpt() will result in a mere "Hi, this is my post!" output,
despite it being a lot shorter than 55 words.

Figure 3-2: The Excerpt box on the Write Post/Add New Post screen in the WordPress admin.

What good is the_excerpt(), then? It may be useful for lists where you don’t want the
blog posts to take up so much space, such as in search results or perhaps some kind of archive
page. However, the most obvious usage would be as an alternative way of promoting a post.
You can have a separate loop that would feature posts with a specific tag and display their
titles as usual, but then just output the_excerpt() rather than the content. In fact, I’ll
show you how in the upcoming example.

For clarity’s sake, this is the default usage of the_excerpt():

<?php the_excerpt(); ?>
Free download pdf