WordPress:第一篇文章完整,其他人显示摘录


WordPress: First post in full, others show excerpts

我已经设置了一个WordPress模板来完整显示第一篇文章,其他文章作为摘录:http://growthgroup.com/testing-blog

当您转到帖子的第 2 页时,它会完整显示第一篇文章,其他帖子也显示为摘录,但我只希望最近的帖子是完整的帖子,而所有其他帖子都是摘录。

有没有办法解决这个问题?这是我正在使用的代码:

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

如果我理解正确,我认为这可能会做你所追求的:

<?php if ($postcount == 1 && $paged == 1) : // if this is the first post & first page ?>

这应该仅在第一页的第一篇文章上触发上部条件。

Wordpress 版本 3.1 及更高版本使用模板部件。这是获得相同结果的更新方法(具有 Twentyteen 主题)。代码的第一部分位于 index.php,此处的代码位于 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 )
    ) );
}

这也是自定义主题的改编版本:

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

基本上,这组代码的作用是首先检查它是否是唯一的帖子,其中它将摘录设置为内容。然后,如果不是这样,那么它将运行"如果帖子计数等于 0,则显示内容。如果两个规则都不符合,则显示摘录。

值得注意的是,在自定义版本中,标题有自己的代码行。

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