Using WordPress’ Excerpt (the_excerpt) Creatively

WPShout

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 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?

You should follow me on Twitter

Related Posts

Display Quotes From A Random Post

How To: Display Quotes From A Random Post in WordPress


YARPP and the_excerpt_rss Don't Mix

Warning: YARPP and the_excerpt_rss Don’t Mix


WPShift

5 Comments - Add Yours!

  • Epic Alex wrote on July 8, 2009 at 2:51 pm | Permalink

    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?

    • Alex Denning wrote on July 16, 2009 at 5:02 pm | Permalink

      You could well have a point there. Looks like we’re all stuck with custom fields for a while!

    • Charles Nibbana wrote on September 7, 2009 at 1:35 pm | Permalink

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

      • edouard wrote on September 15, 2009 at 5:14 pm | Permalink

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

        • edouard wrote on September 15, 2009 at 5:15 pm | Permalink

          using the_excerpt_rss() is working I mean …

Post a Comment

Please take not of the comment policy.

Your email is never published nor shared.

*
*
User Gravatar