Using WordPress’ Excerpt (the_excerpt) Creatively

Using WordPress’ the_excerpt function, or that box that says ‘Excerpt’ creatively isn’t something that too many people do. Why not? Not sure. But maybe that will change – the_excerpt can do so much so easily.

Display a quote from an article

Something that I used to do on Nometet was have a blog-like layout with a quote from the article displayed below the title. It’s something that gives the reader a quick peek at what’s in store in the article and well, looks pretty cool! What’s more, it’s easy to do – just pop a quote into the excerpt box when you’re writing a post. To implement your exciting new quote, just pop <?php the_excerpt(); /> into your theme’s files.

Display a random quote in your header/sidebar/footer

Now that we’re displaying random posts on our homepage (as shown above), next thing we can do the_excerpt is show a random quote on our header/sidebar/footer. Assuming you’ve filled out the Excerpt field for all your posts, the following code will display a random quote, with a link to the article:

[php]&lt;?php query_posts(array(‘orderby’ =&gt; ‘rand’, ‘showposts’ =&gt; 1));

if (have_posts()) : while (have_posts()) : the_post();

&lt;a href=”&lt;?php the_permalink(); ?&gt;”&gt;the_excerpt();&lt;/a&gt;

endwhile; endif; ?&gt;[/php]

Give your posts an introduction

fireshot-pro-capture-12-catswhocode_com-www_catswhocode_com_blog

Here’s an interesting one that I noticed in use on CatsWhoCode (see above)- on the homepage there’s a nice little intro about the post, but visit a post page and you’ll see the very same intro. It’s the same thing, using the_excerpt. Clever, eh? As it also splits your content up, you can pop in an advert in between the two (see it on this post). Just take out your opening section of your post and pop it into the Excerpt field. Then, using <?php the_excerpt(); ?>, implement it in your theme. Word of warning though, the excerpt won’t be shown in your RSS feed, so RSS readers could miss out on your nice little intro.

Avoid using custom fields for images

With the rise of magazine themes, the accompanying annoyance is filling out custom fields for images. This little trick will save some a couple of seconds – just pop the url of the image into the excerpt field (assuming your theme isn’t already using the excerpt) and with the code below, you can get around the need for custom fields:

[php]&lt;img src=”&lt;?php the_excerpt(); ?&gt;”

alt=”&lt;?php the_title(); ?&gt;” /&gt;[/php]

And that’s it

There we go. In this post, we’ve learnt some pretty awesome techniques, but why not share your favourite excerpt technique below?


6 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Free WordPress (Portfolio/Personal Blog) Theme: Happy Go Try | WPShout.com
December 1, 2009 7:13 pm

[…] them!), just a couple of things to know. The text pulled on the front page – that’s the excerpt. The image used can be put in place as with Biblioteca, as the screencast shows (link if you […]

Epic Alex
July 8, 2009 2:51 pm

Does the tip "Display a random quote in your header/sidebar/footer" not create non valid code? the_excerpt outputs p tags, which is a block-level element, and as far as I understood it, that can't go inside an inline element, ie the link.

The same goes for avoiding custom fields, except here doesn't that also mean the scr attribute has p tags in it?

Charles Nibbana
September 7, 2009 1:35 pm
Reply to  Epic Alex

You can solve that by using “the_excerpt_rss()”, which strips the p tags.
.-= Charles Nibbana´s last blog ..My Summer =-.

edouard
September 15, 2009 5:14 pm

oh yeah … cool trick to pass html validation .. that rocks, thx a lot

edouard
September 15, 2009 5:15 pm
Reply to  edouard

using the_excerpt_rss() is working I mean …