【问题标题】:WordPress: First post in full, others show excerptsWordPress:完整的第一篇文章,其他显示摘录
【发布时间】:2013-10-07 16:45:02
【问题描述】:

我已经设置了一个 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 wordpress


    【解决方案1】:

    如果我理解正确,我认为这可能会满足您的要求:

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

    这应该只在第一页的第一个帖子上触发上部条件。

    【讨论】:

      【解决方案2】:

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

      Source

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

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

      【讨论】: