19
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Wordpressのpage.php で if ( have_posts() ) した後に while ( have_posts() ) する理由

Last updated at Posted at 2013-10-09

Wordpressの記事表示部分が非常に気持ち悪かったので聞いたり調べたりしてみました。

      <?php 
      if (have_posts()) :
		     while (have_posts()) :
				       the_post();
      ?>
      <h1 class="page_ttl"><?php the_title(); ?></h1>
      <?php the_content(); ?>
      <?php 
      endwhile;
      endif;
      ?>

気になった点は while(have_posts()) がそもそも真偽値チェックをしてるのに何で if(have_posts()) が必要なのかという点です。「二度手間じゃねえか。」と思うわけです。

実際に調べてみたところこの書き方なら if 部分は要りません。下記のように if ~ else ~ endif を使う場合にのみ必要なようです。

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>

ただこれだと無駄なネストがあるので

<?php if ( !have_posts()) : ?>
<p>Postが一つもないですよ〜。エラーですよ〜。</p>
<?php endif; ?>

<?php while ( have_posts()) : the_post(); ?>
ここ本文を書きましょう。
<?php endwhile; ?>

の方が絶対に良いと思うんです。ネストも減るし何をしてるかわかりやすいですから。
何でこうなってないんでしょう?

ちなみに「404.php があれば elseはいらないよ、大体の場合は。」なアドバイスももらったんですが正直良くわかっていません。

あと Wordpress Codex の The Loop の項目では if 文付きの方法が紹介されていましたが、
http://codex.wordpress.org/The_Loop

デフォルトテーマのTwentyTwelveとTwentyThirteenでは while 文だけが使われていてif文はないようでした。
…と思ったらpage.phpにはないけど、他のファイルにはif文もあるようです。

./archive.php:		<?php if ( have_posts() ) : ?>
./archive.php:			<?php while ( have_posts() ) : the_post(); ?>
./author.php:		<?php if ( have_posts() ) : ?>
./author.php:			<?php while ( have_posts() ) : the_post(); ?>
./category.php:		<?php if ( have_posts() ) : ?>
./category.php:			<?php while ( have_posts() ) : the_post(); ?>
./index.php:		<?php if ( have_posts() ) : ?>
./index.php:			<?php while ( have_posts() ) : the_post(); ?>
./page.php:			<?php while ( have_posts() ) : the_post(); ?>
./search.php:		<?php if ( have_posts() ) : ?>
./search.php:			<?php while ( have_posts() ) : the_post(); ?>
./single.php:			<?php while ( have_posts() ) : the_post(); ?>
./tag.php:		<?php if ( have_posts() ) : ?>
./tag.php:			<?php while ( have_posts() ) : the_post(); ?>
./taxonomy-post_format.php:		<?php if ( have_posts() ) : ?>
./taxonomy-post_format.php:			<?php while ( have_posts() ) : the_post(); ?>

以上のファイルの該当部分をすべて確認した結果
if 文と while 文の間に header を入れるか、 else 文を使用していました。やはり不必要なようですね。
header 入れるにしても無駄にネストを増やすのは非常に気持ち悪いです。始めたばっかりですが早速Wordpressが嫌いになりそうで困りました。

stackexchange での熟練者な方の話と食い違いますが、いくら考えても自分のほうが正しいと思うので俺が何かを言えるぐらいの経験を積んでからもう一度読んでみることにします。

自分なりの結論

else 使うなら先にエラー処理をする
header を付けたい時も先に処理をする。
WordpressのCodexのプログラミングの作法が気に食わない。
Twentythirtyとかのデフォルトのテンプレートの記述の作法も気に食わない。
でもやっぱりそれでもWordpressはめちゃくちゃ便利なので女騎士&オーク的なノリで使うことにする。

参考

loop - Why should I put if(have_posts())? while(have_posts()) is not enough? - WordPress Answers
http://wordpress.stackexchange.com/questions/117219/why-should-i-put-ifhave-posts-whilehave-posts-is-not-enough

19
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?