特定カテゴリーのみsingle.phpのテンプレートを変更する場合、投稿画面でテンプレートを変更する事もできますが、いちいち変更するのは面倒なので、自動で特定のsingle.phpを読み込む方法です。
テンプレート変更したいカテゴリーのスラッグがnewsの場合
1.まず変更用のsingle.phpを作成します。ファイル名をsingle-news.phpとする。
2.functions.phpに下記を追加します。
// シングルページで適用するテンプレートを変更
function custom_template_include( $template ) {
if ( is_single() && in_category( 'news' ) ) {
$new_template = locate_template( array( 'single-news.php' ) );
if ( '' != $new_template ) {
return $new_template ;
}
}
return $template;
}
add_filter( 'template_include', 'custom_template_include', 99 );
in_category( 'news' ) //この部分にカテゴリースラッグ名を記入
$new_template = locate_template( array( 'single-news.php' ) ); // この部分に変更したいsingle.php名を記入