Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

User:MDAWaffe/My Sandbox

Excerpt Example

The only thing that changes is the main page.

If there is an explicitly defined excerpt, show it INSTEAD of the content on the main page ONLY (and any other page that uses index.php. See Template Hierarchy.

If there is NOT an explicitly defined excerpt, do everything normally.

When the_excerpt is shown, only show a few tags (author, title) instead of the whole nine yards, AND give that post some convenient CSS classes so that posts showing the_excerpt can be styled differently than posts showing the_content.

Change Included index.php (Kubrick)

Backup your original file.

I've changed some of the tabbing and linebreaking for improved legibility. I also took out the title="..." from the link to make the line fit on the page. You should put it back manually if you want it.

What follows is the a new Loop for index.php. Delete The Loop (all of it, including its first if and its last endif) from the file and replace it with this.

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

     <h2 id="post-<?php the_ID(); ?>">
      <a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a>
     </h2>

     <small>By <?php the_author() ?></small>

     <div class="entry">
      <?php the_excerpt(); ?>
     </div>

     <p class="postmetadata"><?php edit_post_link('Edit','',''); ?></p>
 
     <!-- <?php trackback_rdf(); ?> -->

    </div>

   <?php else : // If there is not an explictly defined excerpt ?>

       <div class="content-post">

        <h2 id="post-<?php the_ID(); ?>">
         <a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a>
        </h2>

        <small><?php the_time('F jS, Y') ?> by <?php the_author() ?></small>

        <div class="entry">
         <?php the_content('Read the rest of this entry &raquo;'); ?>
        </div>

        <p class="postmetadata">
         Posted in <?php the_category(', ') ?>
         <strong>|</strong>
         <?php edit_post_link('Edit','','<strong>|</strong>'); ?>
         <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?>
        </p>

        <!-- <?php trackback_rdf(); ?> -->

       </div>

 <?php endif; // End the excerpt vs. content "if" statement ?> 
  <?php endwhile; // End the "while" part of the loop ?>
  <div class="navigation">
   <div class="alignleft"><?php posts_nav_link('','','&laquo; Previous Entries') ?></div>
   <div class="alignright"><?php posts_nav_link('','Next Entries &raquo;','') ?></div>
  </div>

 <?php else : // If there weren't any posts at all to display (the very first "if") ?>

  <h2 class="center">Not Found</h2>

  <p class="center">
   <?php _e("Sorry, but you are looking for something that isn't here."); ?>
  </p>

  <?php include (TEMPLATEPATH . "/searchform.php"); ?>

 <?php endif; // End the "if" part of the loop ?>