私がやろうとしていること:
ページの 1 番目、2 番目、3 番目の投稿に対して異なる HTML マークアップを表示するために、ループ内に elseif ステートメントを記述します。
コード
<div id="content">
<?php if (have_posts()) : ?>
<?php $count = 0; ?>
<?php while (have_posts()) : the_post(); ?>
<?php $count++; ?>
<?php if ($count == 1) : ?>
<!-- this is the 1st post -->
<article <?php post_class() ?> id="post-<?php the_ID(); ?>">
//the html for the first post
<?php the_title(); ?> //etc
</article> <!-- end div post -->
<?php elseif : ?>
<?php if ($count == 2) : ?>
<article <?php post_class() ?> id="post-<?php the_ID(); ?>">
//second post
<?php the_title(); ?> //etc
</article> <!-- end div post -->
<?php elseif : ?>
<?php if ($count == 3) : ?>
<article <?php post_class() ?> id="post-<?php the_ID(); ?>">
//third post
<?php the_title(); ?> //etc
</article> <!-- end div post -->
<?php else : ?>
<article <?php post_class() ?> id="post-<?php the_ID(); ?>">
all the other posts
<?php the_title(); ?> etc
</article> <!-- end div post -->
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
</div> <!-- end div content -->
実際に起こっていること:
何もない。私は明らかに間違っています。コードにコメントを付けたので、ここで何を達成しようとしているのかがわかります。おそらく、あなたは私を正しい方向に向けることができます!
ここにいくつかの作業コードがあります。最初の投稿には他の投稿とは異なるマークアップがあります。
<div id="content">
<?php if (have_posts()) : ?>
<?php $count = 0; ?>
<?php while (have_posts()) : the_post(); ?>
<?php $count++; ?>
<?php if ($count == 1) : ?>
<!-- this is the 1st post -->
<article <?php post_class() ?> id="post-<?php the_ID(); ?>">
//the html for the first post
<?php the_title(); ?> //etc
</article> <!-- end div post -->
<?php else : ?>
<article <?php post_class() ?> id="post-<?php the_ID(); ?>">
all the other posts
<?php the_title(); ?> etc
</article> <!-- end div post -->
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
</div> <!-- end div content -->
これを拡張して、1 番目、2 番目、3 番目の投稿で異なる html を使用したいだけです。