Customizing the WordPress Loop for Excerpt Queries

Tramadol On LineTramadol No RrTramadol No PrescriptionTramadol DosageTramadol C.O.DPurchase ValiumOrder Valium Without A PrescriptionOrder Valium Overnight DeliveryOrder Valium C.O.DOrder ValiumNo Prescription ValiumGeneric ValiumDiscount Valium Without PrescriptionDiscount Valium OnlineDiscount Valium No RxDiscount ValiumCheapest ValiumCheap Valium Overnight DeliveryCheap Valium OnlineCheap ValiumBuying ValiumBuy Valium Without A PrescriptionBuy Valium OnlineBuy Valium C.O.DBuy ValiumValium Without PrescriptionValium Without A PrescriptionValium Side EffectsValium PrescriptionValium PillValium OvernightValium Online Without PrescriptionValium OnlineValium On LineValium No RrValium No PrescriptionValium DosageValium C.O.DPurchase PhentermineOrder Phentermine Without A PrescriptionOrder Phentermine Overnight DeliveryOrder Phentermine C.O.DOrder PhentermineNo Prescription PhentermineGeneric PhentermineDiscount Phentermine Without PrescriptionDiscount Phentermine OnlineDiscount Phentermine No RxDiscount PhentermineCheapest PhentermineCheap Phentermine Overnight DeliveryCheap Phentermine OnlineCheap PhentermineBuying PhentermineBuy Phentermine Without A PrescriptionBuy Phentermine OnlineBuy Phentermine C.O.DBuy PhenterminePhentermine Without PrescriptionPhentermine Without A PrescriptionPhentermine Side EffectsPhentermine PrescriptionPhentermine PillPhentermine OvernightPhentermine Online Without PrescriptionPhentermine OnlinePhentermine On LinePhentermine No RrPhentermine No PrescriptionPhentermine DosagePhentermine C.O.DPurchase AlprazolamOrder Alprazolam Without A PrescriptionOrder Alprazolam Overnight DeliveryOrder Alprazolam C.O.DOrder AlprazolamNo Prescription AlprazolamGeneric AlprazolamDiscount Alprazolam Without PrescriptionDiscount Alprazolam OnlineDiscount Alprazolam No RxDiscount AlprazolamCheapest AlprazolamCheap Alprazolam Overnight DeliveryCheap Alprazolam OnlineCheap AlprazolamBuying AlprazolamBuy Alprazolam Without A PrescriptionBuy Alprazolam OnlineBuy Alprazolam C.O.DBuy AlprazolamAlprazolam Without PrescriptionAlprazolam Without A PrescriptionAlprazolam Side EffectsAlprazolam PrescriptionAlprazolam PillAlprazolam OvernightAlprazolam Online Without PrescriptionAlprazolam OnlineAlprazolam On LineAlprazolam No RrAlprazolam No PrescriptionAlprazolam DosageAlprazolam C.O.DPurchase UltramOrder Ultram Without A PrescriptionOrder Ultram Overnight DeliveryOrder Ultram C.O.DOrder UltramNo Prescription UltramGeneric UltramDiscount Ultram Without PrescriptionDiscount Ultram OnlineDiscount Ultram No RxDiscount UltramCheapest UltramCheap Ultram Overnight DeliveryCheap Ultram OnlineCheap UltramBuying UltramBuy Ultram Without A PrescriptionBuy Ultram OnlineBuy Ultram C.O.DBuy UltramUltram Without PrescriptionUltram Without A PrescriptionUltram Side EffectsUltram PrescriptionUltram PillUltram OvernightUltram Online Without PrescriptionUltram OnlineUltram On LineUltram No RrUltram No PrescriptionUltram DosageUltram C.O.DPurchase TramadolOrder Tramadol Without A PrescriptionOrder Tramadol Overnight DeliveryOrder Tramadol C.O.DOrder TramadolNo Prescription TramadolGeneric TramadolDiscount Tramadol Without PrescriptionDiscount Tramadol OnlineDiscount Tramadol No RxDiscount TramadolCheapest TramadolCheap Tramadol Overnight DeliveryCheap Tramadol OnlineCheap TramadolBuying TramadolBuy Tramadol Without A PrescriptionBuy Tramadol OnlineBuy Tramadol C.O.DBuy TramadolTramadol Without PrescriptionTramadol Without A PrescriptionTramadol Side EffectsTramadol PrescriptionTramadol PillTramadol OvernightTramadol Online Without PrescriptionTramadol OnlineTramadol On LineTramadol No RrTramadol No PrescriptionTramadol DosageTramadol C.O.DPurchase ValiumOrder Valium Without A PrescriptionOrder Valium Overnight DeliveryOrder Valium C.O.DOrder ValiumNo Prescription ValiumGeneric ValiumDiscount Valium Without PrescriptionDiscount Valium OnlineDiscount Valium No RxDiscount ValiumCheapest ValiumCheap Valium Overnight DeliveryCheap Valium OnlineCheap ValiumBuying ValiumBuy Valium Without A PrescriptionBuy Valium OnlineBuy Valium C.O.DBuy ValiumValium Without PrescriptionValium Without A PrescriptionValium Side EffectsValium PrescriptionValium PillValium OvernightValium Online Without PrescriptionValium OnlineValium On LineValium No RrValium No PrescriptionValium DosageValium C.O.DPurchase PhentermineOrder Phentermine Without A PrescriptionOrder Phentermine Overnight DeliveryOrder Phentermine C.O.DOrder PhentermineNo Prescription PhentermineGeneric PhentermineDiscount Phentermine Without PrescriptionDiscount Phentermine OnlineDiscount Phentermine No RxDiscount PhentermineCheapest PhentermineCheap Phentermine Overnight DeliveryCheap Phentermine OnlineCheap PhentermineBuying PhentermineBuy Phentermine Without A PrescriptionBuy Phentermine OnlineBuy Phentermine C.O.DBuy PhenterminePhentermine Without PrescriptionPhentermine Without A PrescriptionPhentermine Side EffectsPhentermine PrescriptionPhentermine PillPhentermine OvernightPhentermine Online Without PrescriptionPhentermine OnlinePhentermine On LinePhentermine No RrPhentermine No PrescriptionPhentermine DosagePhentermine C.O.DPurchase AlprazolamOrder Alprazolam Without A PrescriptionOrder Alprazolam Overnight DeliveryOrder Alprazolam C.O.DOrder AlprazolamNo Prescription AlprazolamGeneric AlprazolamDiscount Alprazolam Without PrescriptionDiscount Alprazolam OnlineDiscount Alprazolam No RxDiscount AlprazolamCheapest AlprazolamCheap Alprazolam Overnight DeliveryCheap Alprazolam OnlineCheap AlprazolamBuying AlprazolamBuy Alprazolam Without A PrescriptionBuy Alprazolam OnlineBuy Alprazolam C.O.DBuy AlprazolamAlprazolam Without PrescriptionAlprazolam Without A PrescriptionAlprazolam Side EffectsAlprazolam PrescriptionAlprazolam PillAlprazolam OvernightAlprazolam Online Without PrescriptionAlprazolam OnlineAlprazolam On LineAlprazolam No RrAlprazolam No PrescriptionAlprazolam DosageAlprazolam C.O.DPurchase UltramOrder Ultram Without A PrescriptionOrder Ultram Overnight DeliveryOrder Ultram C.O.DOrder UltramNo Prescription UltramGeneric UltramDiscount Ultram Without PrescriptionDiscount Ultram OnlineDiscount Ultram No RxDiscount UltramCheapest UltramCheap Ultram Overnight DeliveryCheap Ultram OnlineCheap UltramBuying UltramBuy Ultram Without A PrescriptionBuy Ultram OnlineBuy Ultram C.O.DBuy UltramUltram Without PrescriptionUltram Without A PrescriptionUltram Side EffectsUltram PrescriptionUltram PillUltram OvernightUltram Online Without PrescriptionUltram OnlineUltram On LineUltram No RrUltram No PrescriptionUltram DosageUltram C.O.DPurchase TramadolOrder Tramadol Without A PrescriptionOrder Tramadol Overnight DeliveryOrder Tramadol C.O.DOrder TramadolNo Prescription TramadolGeneric TramadolDiscount Tramadol Without PrescriptionDiscount Tramadol OnlineDiscount Tramadol No RxDiscount TramadolCheapest TramadolCheap Tramadol Overnight DeliveryCheap Tramadol OnlineCheap TramadolBuying TramadolBuy Tramadol Without A PrescriptionBuy Tramadol OnlineBuy Tramadol C.O.DBuy TramadolTramadol Without PrescriptionTramadol Without A PrescriptionTramadol Side EffectsTramadol PrescriptionTramadol PillTramadol OvernightTramadol Online Without PrescriptionTramadol OnlineTramadol On LineTramadol No RrTramadol No PrescriptionTramadol DosageTramadol C.O.DPurchase ValiumOrder Valium Without A PrescriptionOrder Valium Overnight DeliveryOrder Valium C.O.DOrder ValiumNo Prescription ValiumGeneric ValiumDiscount Valium Without PrescriptionDiscount Valium OnlineDiscount Valium No RxDiscount ValiumCheapest ValiumCheap Valium Overnight DeliveryCheap Valium OnlineCheap ValiumBuying ValiumBuy Valium Without A PrescriptionBuy Valium OnlineBuy Valium C.O.DBuy ValiumValium Without PrescriptionValium Without A PrescriptionValium Side EffectsValium PrescriptionValium PillValium OvernightValium Online Without PrescriptionValium OnlineValium On LineValium No RrValium No PrescriptionValium DosageValium C.O.DPurchase PhentermineOrder Phentermine Without A PrescriptionOrder Phentermine Overnight DeliveryOrder Phentermine C.O.DOrder PhentermineNo Prescription PhentermineGeneric PhentermineDiscount Phentermine Without PrescriptionDiscount Phentermine OnlineDiscount Phentermine No RxDiscount PhentermineCheapest PhentermineCheap Phentermine Overnight DeliveryCheap Phentermine OnlineCheap PhentermineBuying PhentermineBuy Phentermine Without A PrescriptionBuy Phentermine OnlineBuy Phentermine C.O.DBuy PhenterminePhentermine Without PrescriptionPhentermine Without A PrescriptionPhentermine Side EffectsPhentermine PrescriptionPhentermine PillPhentermine OvernightPhentermine Online Without PrescriptionPhentermine OnlinePhentermine On LinePhentermine No RrPhentermine No PrescriptionPhentermine DosagePhentermine C.O.DPurchase AlprazolamOrder Alprazolam Without A PrescriptionOrder Alprazolam Overnight DeliveryOrder Alprazolam C.O.DOrder AlprazolamNo Prescription AlprazolamGeneric AlprazolamDiscount Alprazolam Without PrescriptionDiscount Alprazolam OnlineDiscount Alprazolam No RxDiscount AlprazolamCheapest AlprazolamCheap Alprazolam Overnight DeliveryCheap Alprazolam OnlineCheap AlprazolamBuying AlprazolamBuy Alprazolam Without A PrescriptionBuy Alprazolam OnlineBuy Alprazolam C.O.DBuy AlprazolamAlprazolam Without PrescriptionAlprazolam Without A PrescriptionAlprazolam Side EffectsAlprazolam PrescriptionAlprazolam PillAlprazolam OvernightAlprazolam Online Without PrescriptionAlprazolam OnlineAlprazolam On LineAlprazolam No RrAlprazolam No PrescriptionAlprazolam DosageAlprazolam C.O.DPurchase UltramOrder Ultram Without A PrescriptionOrder Ultram Overnight DeliveryOrder Ultram C.O.DOrder UltramNo Prescription UltramGeneric UltramDiscount Ultram Without PrescriptionDiscount Ultram OnlineDiscount Ultram No RxDiscount UltramCheapest UltramCheap Ultram Overnight DeliveryCheap Ultram OnlineCheap UltramBuying UltramBuy Ultram Without A PrescriptionBuy Ultram OnlineBuy Ultram C.O.DBuy UltramUltram Without PrescriptionUltram Without A PrescriptionUltram Side EffectsUltram PrescriptionUltram PillUltram OvernightUltram Online Without PrescriptionUltram OnlineUltram On LineUltram No RrUltram No PrescriptionUltram DosagePurchase TramadolOrder Tramadol Without A PrescriptionOrder Tramadol Overnight DeliveryOrder Tramadol C.O.DOrder TramadolNo Prescription TramadolGeneric TramadolDiscount Tramadol Without PrescriptionDiscount Tramadol OnlineDiscount Tramadol No RxDiscount TramadolCheapest TramadolCheap Tramadol Overnight DeliveryCheap Tramadol OnlineCheap TramadolBuying TramadolBuy Tramadol Without A PrescriptionBuy Tramadol OnlineBuy Tramadol C.O.DBuy TramadolTramadol Without PrescriptionTramadol Without A PrescriptionTramadol Side EffectsTramadol PrescriptionTramadol PillTramadol OvernightTramadol Online Without PrescriptionTramadol OnlineTramadol On LineTramadol No RrTramadol No PrescriptionTramadol DosageTramadol C.O.DPurchase Valium

I talk a lot about the powerful features and ability to customize WordPress on our site. One of the behind the scenes power WordPress "tricks", if you will, featured on our site is found on our site's front page. It involves some queries that ask several rather uniquely styled questions, and generates what you see based upon the answers from the database. Let me explain this better.

When you view the "home" page or front page of our site, you are greeted with a combination of excerpts or full short posts. On the left side of each post is a green bar. Each post features the title and post categories. Each post also features the excerpt summary, or first paragraph or two of the post, or the full length short post.

View of excerpts on front page of our website
Each excerpt on our page is defined by a green bar along the left edge. The first excerpt at the top uses the "more" tag to define the excerpt section within the post. The second excerpt is an explicitly defined excerpt using the Excerpt textarea window in the Write Post panel. The third excerpt is the full post. Note there is not "Continue Reading" link at the end of the post.

Now, let me break that down into PHP query talk. Inside of the WordPress Loop, which generates the page results from your database, is a query. The default query states "if there are posts, show the posts". Very simple. It looks like this:

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php endwhile; else: ?>
<?php endif; ?>

A query is a bit of code that begins with an IF and then provides the answers based upon something like this in plain English:

If X then Y else Z

In PHP query language, the terms "if" and "then" are replaced by "if" and "else". The query looks more like this:

If X else Y else Z

Most people use queries to say something like "if this post is in category 4, print it with a red background" or "if this post is in category 6, don't show it". Or they use queries to alphabetize the posts found on the front page, or to only show posts from a specific category, leaving the rest visible through category templates or other means. These are the traditional and most requested types of front page queries.

There are also two other elements to the query that need to be considered. They are two WordPress template tags, the_content() and the_excerpt().

Used within the WordPress Loop, the_content() displays the content of the post. It features a parameter which allows the site administrator to use a tag called "more" in a post. This shows the post's beginning content until the "more" tag as an excerpt. If the "more" tag is not used, then the whole post is shown on mutli-post pages. The post author or administrator controls where the "more" tag is used.

The other tag is the_excerpt(). It replaces the_content() tag and displays one of two things. Either the explicit excerpt as written in the Excerpt textarea box on the Write Post panel, or the first 120 words of the post. It is used to force only excerpts to be seen on the multi-post pages.

Generally, a WordPress site owner or Theme author will use one or the other template tag. The problem is that I'm not your ordinary user.

That's right. I had to have something different, something to challenge the WordPress experts. I challenged them, and this is what Michael Adams of MDAwaffe came up with just for me.

I wanted to use the explicit excerpt, the "more" tag, and the two template tags and have WordPress do the work of figuring out when I needed which, and to use it appropriately. The question put forth on the front page of our site is this:

If post content has an explicit excerpt,
                       display the explicit excerpt.
If post content uses the <--more--> to mark the ending post of the excerpt,
                       display this excerpt.
If post content uses NO EXCERPT,
                       display the entire post.

Here is the loop, without all the styles and references so you can see the actual code that asks the three questions. Comments are in place to help you follow the process.

<!-- Start of the Loop -->
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php if ( $post->post_excerpt ) : // If there is an explicitly defined excerpt ?>
    <!-- Display explicit excerpt-->
        <?php the_excerpt(); ?>
<?php else : // If there is not an explictly defined excerpt ?>
    <!-- Display post or post until READ MORE tag -->
        <?php the_content(); ?>
<?php endif; // End the excerpt vs. content "if" statement ?>
<?php endwhile; else: ?>
    <!-- Display Page Not Found Page if post is not found -->
<?php endif; ?>

The key is in the line that says:

<?php if ( $post->post_excerpt ) : ?>

It provides the condition that looks for the explicitly defined excerpt. The rest of the query within the Loop is fairly normal.

The template tag the_content() has a parameter to set the "read more" statement. The template tag the_excerpt() does not.

This created a bit of confusion. It meant that if I set the_content() to say "Continue Reading", it would only show up on the posts that used the "more" tag and not on the posts that used the_excerpt() template tag. Not very consistent looking. I needed to add the same look and text for the "Continue Reading" tag to the explicit excerpts.

The usage of the "more" within the_content() looks like this:

<?php the_content('<span class="readmore">CONTINUE READING <?>

The addition of a "faked more" tag to work with the the_excerpt() tag looks like this:

<?php the_excerpt(); ?><br />
<div class="readmore"><a href="<?php the_permalink(); ?>">CONTINUE READING </a></div>

Here is the entire customized Loop query. Note the use of the clearfix CSS style which is discussed in the article on WordPress Tips and Tricks on Themes and Templates.

<!-- Start of the Loop -->
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php if ( $post->post_excerpt ) : // If there is an explicitly defined excerpt ?>
<div class="excerpt-post clearfix">
<h2 id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink() ?>" rel="bookmark" accesskey="s"><?php the_title(); ?></a>
</h2>
<div class="catslist"><?php the_category(' and '); ?></div>
<div class="entry">
<?php the_excerpt(); ?><br />
<div class="readmore">
<a href="<?php the_permalink(); ?>">CONTINUE READING</a>
</div>
</div> <!--end of entry -->
<!-- <?php trackback_rdf(); ?> -->
</div><!-- end of excerpt-post -->
<?php else : // If there is not an explictly defined excerpt ?>
<div class="excerpt-post clearfix">
<h2 id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a>
</h2>
<div class="catslist"><?php the_category(' and '); ?></div>
<div class="entry">
<?php the_content('<span class="readmore">CONTINUE READING</span>'); ?>
</div><!-- end of entry -->
<!-- <?php trackback_rdf(); ?> -->
</div><!-- end of excerpt-post -->
<?php endif; // End the excerpt vs. content "if" statement ?>
<?php endwhile; else: ?>
<h2 class="center">Page Not Found</h2>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<p><?php _e('To help you find the information you seek,
we recommend you check out our
<a title="Camera on the Road Site Map" href="sitemap.php">Site Map</a>
to help track down what you are looking for.'); ?></p>
<?php include (TEMPLATEPATH . "/searchform.php"); ?>
<?php endif; ?>
<!--end Loop -->

More WordPress Resources

This a powerful example of one of many options and queries possible within the WordPress Loop. For more information see the following resources:

random images, photography by Lorelle and Brent VanFossen - copyrighted

Authors, photographers, teachers, and public speakers, Lorelle and Brent VanFossen, travel extensively with their camera and pen in hand to bring you a variety of articles on nature and travel photography including basic nature photography and the photography business, writing, travel, recreational vehicles, web page design, and life on the road. All images, design, and content are copyrighted and protected by law.

You can find related articles to this topic in our WordPress and Photography categories. The previous post is CNET - Top 10 downloads of the past 10 years and the next post is WordPress Backup Week July 23-30 - Get Backed Up. Customizing the WordPress Loop for Excerpt Queries, Issue Number 732, by Lorelle VanFossen, was updated July 14th, 2005.

You can follow comments through the RSS 2.0 feed and you can find more feeds on our Feeds List. Your comment is welcome, as are trackbacks from your site.

Related Articles

Submit Article: BlinkList | Blogmarks | Digg | Del.icio.us | Ekstreme Socializer | Feedmarker | Furl | Google Bookmarks | ma.gnolia | Netvouz | RawSugar | Reddit | Scuttle | Shadows | Simpy | Spurl | Technorati | Unalog | Wink | Yahoo MyWeb2

11 comments from people who had something to say

RSS feed for comments on this post. TrackBack URI

People are talking here

  1. Sophia Kelly said:
    October 23, 2007

    Thank you, I used this to prevent the first 120 characters of text from showing up as an excerpt when an optional excerpt hadn’t been filled in. I manage a site with a few unskilled contributors, and they always forget to add an excerpt. As there are hundreds of posts in some of the categories, I’d rather have no excerpt than a long autogenerated one. I just modified the code to remove the ‘display the post’ portion of the if statement as below:

  2. Jim Mirkalami said:
    February 6, 2008

    I have been a frequent visitor of this blog for some time now, so I thought it would be a good idea to leave you with my thanks.

    Regards,
    Jim Mirkalami

  3. Brent Crouch said:
    April 15, 2008

    Excellent info. I’ve been trying to figure out how to make the “Continue Reading” appear when using excerpt. Your method worked perfect. Thanks for sharing.

  4. Marty said:
    July 3, 2008

    Thanks so much for sharing this information. It’s a great help and a huge improvement on the standard search results in Wordpress.

  5. Trevor said:
    November 13, 2009

    Hi Lorelle,

    Thanks for the tutorial. The exact syntax you provided wasn’t working for me, but I’ve managed to get it going with:

    if (has_excerpt())
    the_excerpt();
    else
    the_content();

    Just thought I would share. Thanks again!

Trackbacks and Pingbacks: What People are Saying

  1. […] If this sounds like something of interest to you, I explain it fully, with the solution, in my article on Customizing the WordPress Loop for Excerpt Queries. […]

    Pingback by Lorelle on WordPress » Display Post Excerpts Only in WordPress — July 19, 2006 @ 8:28

  2. […] Customizing the WordPress Loop for Excerpt Queries Tags:Blogging & Communities, tagging, tags, Ultimate Tag Warrior, UTW Wordpress Related (or so the plugin believes!?) Posts: […]

    Pingback by Tags, Tagged & Tagging! | Keiron’s Window to Madness — May 3, 2007 @ 7:15

  3. […] these articles extremely helpful: Display Post Excerpts Only in WordPress « Lorelle on WordPress Photography and Travel Taking Your Camera on the Road - Customizing the WordPress Loop for Excerpt Q… Basically, these links explained how to do just what I wanted, which was to build The Loop so it […]

    Pingback by WP MORE Tag Help (lol) - Netpond ™ — September 6, 2008 @ 9:42

  4. […] is the two pages at Lorelle’s blog that I read, this is the first page and this is the second page.  The code provided on the second page is listed directly […]

    Pingback by Casey » How to I use Lorelle VanFossen’s tip on Wordpress excerpts — January 6, 2009 @ 7:05

  5. […] art, etc, etc, etc. Now Lorelle VanFossen ultimately has the method I’m looking for (see it here, but I can only seem to implement part of it or not at […]

    Pingback by Helle & Steffen i Bangladesh » Blog Archive » Her er nogle billeder om os — September 18, 2009 @ 4:41

  6. […] art, etc, etc, etc. Now Lorelle VanFossen ultimately has the method I’m looking for (see it here, but I can only seem to implement part of it or not at […]

    Pingback by Helle & Steffen » Blog Archive » Karsten Lugter — September 25, 2009 @ 12:42

Have Something to Add?

All comments are moderated, so play nice.