0

I have a page with three excerpts. Only the first excerpt is being given the post class, not the second or third. I presume something is wrong in my PHP:

      <table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">
       <tr>
        <td width="100%" valign="top"> 
         <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
          <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
           <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
          <?php the_excerpt(); ?>
          <p><?php the_time('F j, Y'); ?> at <?php the_time('g:i a'); ?> | <?php the_category(', '); ?> | 
          <?php comments_number('No comment', '1 comment', '% comments'); ?></p>
         </div>
         <?php endwhile; else: ?>
         <h2>Oops...</h2>
         <p>Sorry, no posts we're found.</p>
         <?php endif; ?>
         <p align="center"><?php posts_nav_link(); ?></p>
        </td>
       </tr>
      </table>

Here is the resulting HTML:

<table width="100%" cellspacing="0" cellpadding="0" border="0" align="center">

<tbody>
    <tr>
        <td width="100%" valign="top">
            <div id="post-19" class="post-19 post type-post status-publish format-standard hentry category-printables">
                <h2>
                    <a href="myurl"></a>
                </h2>
                <p></p>
                <p></p>
            </div>
            <h2></h2>
            <p></p>
            <p></p>
            <h2></h2>
            <p></p>
            <p></p>
            <p align="center"></p>
        </td>
    </tr>
</tbody>

How do I ensure that the post class is applied to every single excerpt, not just the first one?

  • 1
    everything between while and endwhile gets repeated for each post, that's the loop. the thing you want repeated is outside that loop. – Milo May 11 '15 at 16:59
1

Try this way

          <table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">
   <tr>
    <td width="100%" valign="top"> 
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
     <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
     <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
      <?php the_excerpt(); ?>
      <p><?php the_time('F j, Y'); ?> at <?php the_time('g:i a'); ?> | <?php the_category(', '); ?> | 
      <?php comments_number('No comment', '1 comment', '% comments'); ?></p>
     </div>
     <?php endwhile; else: ?>
     <h2>Oops...</h2>
     <p>Sorry, no posts we're found.</p>
     <?php endif; ?>
     <p align="center"><?php posts_nav_link(); ?></p>
    </td>
   </tr>
  </table>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.