Reputation: 459
I've set up a WordPress template to show the first post in full, and the others as excerpts: http://growthgroup.com/testing-blog
When you go to Page 2 of posts, it shows the first post in full, and the others as excerpts too, but I would only like the most recent post to be the full post, and ALL the others to be excerpts.
Is there a way around this? Here is the code I am using:
<?php
// query to set the posts per page to 5
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('posts_per_page' => 5, 'paged' => $paged );
query_posts($args); ?>
<?php if (have_posts()) : ?>
<?php $postcount = 0; // Initialize the post counter ?>
<?php while (have_posts()) : the_post(); //start the loop ?>
<?php $postcount++; //add 1 to the post counter ?>
<?php if ($postcount == 1) : // if this is the first post ?>
<div class="post">
<h3 class="post-title"><a href="<?php the_permalink();?>" title="<?php the_title();?>"><?php the_title(); ?></a></h3>
<span class="author-date-blog"><?php the_author();?> | <?php the_date('d M Y');?></span>
<div class="excerpt">
<?php the_content();?>
<div class="read-more">
<a href="<?php the_permalink();?>#disqus_thread">Leave a Comment >></a>
</div>
</div>
</div>
<?php else : //if this is NOT the first post ?>
<div class="post">
<div class="thumb">
<a href="<?php the_permalink();?>" title="<?php the_title();?>"><?php the_post_thumbnail('blog-thumb');?></a>
</div>
<h3 class="post-title"><a href="<?php the_permalink();?>" title="<?php the_title();?>"><?php the_title(); ?></a></h3>
<span class="author-date-blog"><?php the_author();?> | <?php the_date('d M Y');?></span>
<div class="excerpt">
<?php the_excerpt(); ?>
<div class="read-more">
<a href="<?php the_permalink();?>">Read More >></a>
</div>
</div>
</div>
<?php endif; endwhile; //end of the check for first post - other posts?>
Upvotes: 3
Views: 2215
Reputation: 131
Wordpress Version 3.1 and on utilizes template Parts. Here's an updated method to getting the same result(with Twentyfifteen theme). The first part of the code is located in index.php, and this code here is in content.php.
if ($wp_query->current_post == 0) {
/* translators: %s: Name of current post */
the_content( sprintf(
__( 'Continue reading %s', 'twentyfifteen' ),
the_title( '<span class="screen-reader-text">', '</span>', false )
) );
}
else {
/* translators: %s: Name of current post */
the_excerpt( sprintf(
__( 'Continue reading %s', 'twentyfifteen' ),
the_title( '<span class="screen-reader-text">', '</span>', false )
) );
}
Here's an adapted version for a custom theme too:
<div class="entry-content">
<?php
if (is_single()) :
the_content();
//}
elseif ($wp_query->current_post == 0) :
/* translators: %s: Name of current post */
the_content();
else :
the_excerpt();
echo '<a class="read_more" href="' . esc_url(get_permalink()) . '">' . __('Read more', 'imelton') . '</a>';
endif;
?>
Basically, What this set of code does, is first check to see if it is the ONLY post, in which it sets the excerpt to the content. Then, if that isn't true, then it will run the code for "If the post count is equal to 0, then display the content. If both rules are not met, then display the excerpt."
It is worth noting that in the custom version, the title has its own code line.
<header class="entry-header">
<?php
if (is_single()) :
the_title('<h1 class="entry-title">', '</h1>');
else :
the_title(sprintf('<h2 class="entry-title"><a href="%s" rel="bookmark">', esc_url(get_permalink())), '</a></h2>');
endif;
?>
<div class="post_date"><?php the_date('l, F j, Y - h:i', '', ''); ?></div>
Upvotes: 4
Reputation: 2333
If I'm understanding correctly, I think this might do what you're after:
<?php if ($postcount == 1 && $paged == 1) : // if this is the first post & first page ?>
That should make the upper conditional fire on the first post of the first page only.
Upvotes: 4