WordPress/カテゴリー毎にsingle.phpのデザイン変更
準備
必要なファイルは、[single.php]と変更するデザイン分のファイル。
ここでは2種類のデザインを使い分けたいので、[single1.php]と[single2.php]の2つのファイルを用意。
single.phpに以下のコードを記載
<?php
$post = $wp_query->post;
if ( in_category('カテゴリーID') ) {
include(TEMPLATEPATH.'/single1.php');
} else {
include(TEMPLATEPATH.'/single2.php');
}
?>
“カテゴリーID”の投稿は[single1.php]のデザインを使用。
それ以外の投稿は、[single2.php]のデザインを使用する。
single.phpのコードはこの記述のみ。
あとはsingle1.php、single2.phpにそれぞれ単一ページ用のコードを記述。
もっと使い分けるとき
<?php
$post = $wp_query->post;
if ( in_category('カテゴリーID1') ) {
include(TEMPLATEPATH.'/single1.php');
} elseif ( in_category('カテゴリーID2') ) {
include(TEMPLATEPATH.'/single2.php');
} elseif ( in_category('カテゴリーID3') ) {
include(TEMPLATEPATH.'/single3.php');
} elseif ( in_category('カテゴリーID4') ) {
include(TEMPLATEPATH.'/single4.php');
} else {
include(TEMPLATEPATH.'/single5.php');
}
?>