Different Single Post Pages In WordPress by Category

By December 21, 2015Helps & Tutorials, Wordpress

By default all WordPress posts viewed as a single post are controlled by the particular theme’s “single.php” file.

For every post, WordPress uses (“single.php“) to show the page. But you can use Different Pages (Design/Type) to show different categories. For this you need a bit of PHP coding in the “single.php” file.

Here is your example,

  • Category 3 is my Video Pages
  • Category 4 is a Photo Pages
  • Category 5 is a MP3 section

Here we want to display different layouts according to the particular post’s category in “single.php”.

[code language=”php”]

<?php
$post = $wp_query->post;
if ( in_category(‘2’) ) {
include(TEMPLATEPATH . ‘/single-video.php’); }
elseif ( in_category(’15’) ) {
include(TEMPLATEPATH . ‘/single-photo.php’); }
elseif ( in_category(’18’) ) {
include(TEMPLATEPATH . ‘/single-mp3.php’); }
else {
include(TEMPLATEPATH . ‘/single-main.php’);
}
?>

[/code]

Before doing all these steps please keep a Backup of Original “single.php” file.

Leave a Reply