※以前の質問の内容が明確ではないと指摘をうけましたので内容を編集させていただきました。ご指摘いただきありがとうございます。
wordpressにて下記コードで特定のカテゴリの記事を表示させています。
記事一覧の最初の記事のみを判別して見せ方に変化をつけるにはどうすればよろしいでしょうか?
やりたいこと
記事一覧の最初の記事のみに記事の本文を表示したいです。
ループ1回目←記事本文を追加したい
ループ2回目
ループ3回目
ループ4回目
ループ5回目
現状全て下記のようなコードで一覧表示にしているのですが、
最初のループ時のみ記事の本文を表示するにはどうすればよろしいでしょうか?
<?php $args = array( 'numberposts' => 5, 'order' => 'ASC', 'post_type' => '投稿タイプ', 'tax_query' => array( array( 'taxonomy' => 'タクソノミー名', 'field' => 'slug', 'terms' => 'ターム名' ))); $customPosts = get_posts($args); if($customPosts) : foreach($customPosts as $post) : setup_postdata( $post ); ?> <!--ループする記事の内容--> <table> <tr> <th>タイトル</th> <td><?php the_field(); ?></td> </tr> ... </table> <!--ループする記事の内容ここまで--> <?php endforeach; ?> <?php endif; wp_reset_postdata(); ?>
試したこと
<?php $args = array( 'numberposts' => 5, 'order' => 'ASC', 'post_type' => '投稿タイプ', 'tax_query' => array( array( 'taxonomy' => 'タクソノミー名', 'field' => 'slug', 'terms' => 'ターム名' ))); $customPosts = get_posts($args); $customCount = 0; if($customPosts) : foreach($customPosts as $post) : setup_postdata( $post ); ?> <?php if ( $customCount === 0 ) { ?> <!--1回目--> <table> <tr> <th>タイトル</th> <td><?php the_field(); ?></td> </tr> ... </table> <?php the_content(); ?> <!--ここまで--> <?php } $customCount++; ?> <!--ループする記事の内容--> <table> <tr> <th>タイトル</th> <td><?php the_field(); ?></td> </tr> ... </table> <!--ループする記事の内容ここまで--> <?php endforeach; ?> <?php endif; wp_reset_postdata(); ?>
以前ご回答いただいたことを試させていただいたのですが、書きかたに問題があったのか
記事本文が追加された記事
ループ1回目
ループ2回目
ループ3回目
ループ4回目
ループ5回目
のように計6記事が表示されてしまいました。
回答1件
あなたの回答
tips
プレビュー