0
<?php get_header(); ?>
<div class="page-wrap group">

<div class="grid">

  <?php query_posts('showposts=6'); ?>

    <?php if (have_posts()): ?>

  <section class="blog-posts col-2-3 grid-pad">

    <?php while (have_posts()) : the_post(); ?>

      <article class="module" id="post-<?php the_ID(); ?>">

        <h2> <a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a </h2>
        <p><time datetime="2001-05-15 19:00"><?php echo date('l jS F Y')?></p>

        <?php the_excerpt(); ?>


      </article>

      <?php endwhile; ?>

  </section>

これは、index.php の私のコードです。私がやりたいことは、3 つのブログ投稿を<section class="blog-posts col-2-3 grid-pad">次に 3 つの投稿にすることです。<section class="blog-posts col-2-3 grid-pad">

では、どのように投稿をループしてページ全体に広げることができるのでしょうか? 今はそれしか取得できないので、最初にすべて表示されます

4

1 に答える 1

1

カウンター変数を使用するだけです。

<?php if (have_posts()): ?>

  <section>

  <?php $i = 0; while (have_posts()) : the_post(); ?>

      <article>
      ...
      <article>

  <?php if(++$i === 3): // close previous and open new one ?>
  </section>

  <section>
  <?php endif; ?>      

  <?php endwhile; ?>
  </section>    

<?php endif; ?>

WP_Queryまたは、クラス、posts_per_page=3引数、および2 番目のクエリで引数を使用して 2 つのクエリを実行できoffset=3ます。


ページに表示される投稿の数を変更するには、これを関数ファイルに追加します。

add_action('pre_get_posts', function($query){

  if(is_admin() || !$query->is_main_query())
    return;

  if(is_home()) // or whatever page you want the limit applied to
    $query->set('posts_per_page', 6);

});
于 2013-02-06T14:30:15.750 に答える