2

最初の投稿を完全に表示し、他の投稿を抜粋として表示するように 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?>
4

2 に答える 2

3

Wordpress バージョン 3.1 以降では、テンプレート パーツを使用します。これは、同じ結果を得るための更新された方法です (Twentyfifteen テーマを使用)。コードの最初の部分は 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>
于 2016-05-17T15:12:59.283 に答える
3

私が正しく理解しているなら、これはあなたが求めていることをするかもしれないと思います:

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

これにより、最初のページの最初の投稿でのみ上部の条件付き火災が発生するはずです。

于 2013-10-07T16:47:56.013 に答える