One of the most frequently asked questions by WordPress web developers is: "How can I display only the first post of The Loop in full?". In this article we'll address this question by providing a simple solution to this problem.
The WordPress object that controls the behavior of the Read More feature is called $more
and returns two values: -1
and 0
. With the former value the default behavior is suppressed, while with the latter is preserved.
Here's how you can use it:
<?php global $more; $more = -1;?>
<?php if (have_posts()) : while (have_posts()) : the_post();?>
<article class="post">
<time datetime="<?php the_time('c');?>"><?php the_time('F j, Y');?></time>
<h2 class="post-title"
><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" rel="bookmark">
<?php the_title(); ?></a></h2>
<div class="post-content">
<?php if ($more == -1) {
the_content();
$more = 0;
} else {
the_content(__('Continue Reading'));
}
?>
</div>
</article>
<?php endwhile; else: ?>
<div class="no-results">
<p>Nothing.</p>
</div>
<?php endif; ?>
Now your WordPress Loop will display the first post in full and the other ones with the default Read More link. Just a word of caution: using this feature on your homepage can be troublesome, especially if your latest post is very long.