wordpressのコピペ用ソース、page.phpでの条件分岐

コンディショナルタグを使うと毎回どこかでミスって真っ白な画面を出してしまい、間違ってるところを探すのに無駄な時間をかけてしまうので、それを極力減らすためにコピペ用のソースを用意。

例として挙げたスラッグ名は、「about」は会社のページだったら「会社概要」、ブログだったら「自己紹介」、「inquiry」は「お問い合わせ」によく使ってます。

page.php用のスラッグ名を使った条件分岐 – コンディショナルタグ(Conditional Tag)

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

<?php /* ページスラッグ「about」のとき */ if (is_page('about')) { ?>
<?php while (have_posts()) : the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php edit_post_link('このページを編集','<p class="edit">','</p>'); ?>
</div><!-- .post -->
<?php endwhile; ?>

<?php /* ページスラッグ「inquiry」のとき */ } else if (is_page('inquiry')) { ?>
<?php while (have_posts()) : the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php edit_post_link('このページを編集','<p class="edit">','</p>'); ?>
</div><!-- .post -->
<?php endwhile; ?>

<?php /* それら以外のページのとき */ } else { ?>
<?php while (have_posts()) : the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php edit_post_link('このページを編集','<p class="edit">','</p>'); ?>
</div><!-- .post -->
<?php endwhile; ?>

<?php } ?>

<?php endif; ?>

  • 2009年
  • 12月08日(火)

この記事のタグ

blog comments powered by Disqus