0

I'm using the template part below and would like to only show the excerpt after the first 3 posts. The first 3 posts are formatted differently as "header" posts and thus I'd rather not have an excerpt for those posts.

Could someone help me with the PHP?

<div class="content-block-archive">

     <div class="content-block-archive-thumbnail">
<a href="<?php the_permalink(); ?>"><?php echo get_the_post_thumbnail($post_id, 'large', array('class' => 'alignleft')); ?></a>
     </div>

     <div class="content-block-archive-meta">
<h2 class="content-block-archive-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>

          <div class="content-block-archive-author"> <?php the_author_posts_link(); ?> | <?php echo get_the_date(); ?> 
          </div>

          <div class="content-block-archive-excerpt"><?php the_excerpt(); ?></div>

    </div>
</div>
0

You could add a count variable and count how many times the page has looped and only show the excerpt if the count is 3 or greater.:

<?php

    $Count = 0;
?>
<div class="content-block-archive">

     <div class="content-block-archive-thumbnail">
<a href="<?php the_permalink(); ?>"><?php echo get_the_post_thumbnail($post_id, 'large', array('class' => 'alignleft')); ?></a>
     </div>

     <div class="content-block-archive-meta">
<h2 class="content-block-archive-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>

          <div class="content-block-archive-author"> <?php the_author_posts_link(); ?> | <?php echo get_the_date(); ?> 
          </div>
        <?php 
        if ($Count >= 3) { ?>
            <div class="content-block-archive-excerpt">
                <?php 
                    the_excerpt(); 
                ?>
            </div>
        <?php 
        } 
        $Count ++;
        ?>
    </div>
</div>
  • Lewis, I don't have a flag. I just identify the first three posts with CSS child selectors to format and then have CSS for the rest of the articles. – Tnace Jul 25 at 21:52
  • I've updated the answer to user a count instead of a flag, let me know if that works. – Lewis M Hackfath Jul 25 at 21:57
  • That broke something. No errors in the inspector tool. It loads the header fine then give a Wordpress error "The site is experiencing technical difficulties." – Tnace Jul 25 at 22:27
  • I've updated again, i missed a closing php tag sorry. – Lewis M Hackfath Jul 25 at 22:42
  • That works perfectly, thanks Lewis! – Tnace Jul 25 at 22:53

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.