Enter your keyword

Blog

Use different single post page in WordPress

WordPress, by default  use one "single.php" for every post. if you want to display a different layout for specific categories  post ?

To Use different single post page we use simple php code in theme "single.php" file.and create category specific single.php files in theme folder.

In the below example lets assume the following:

  • Category 2 is Blog
  • Category 3 is Articles section
  • Category 4 is News section

Create category specific  files in them folder  like as:

single-blog.php  (This file use for Blog category posts )

single-articles.php (This file use for Articles category posts )

single-news.php (This file use for News category posts )

single-default.php (This file use for any post that is not in any of the specified categories (2,3,4) will just use a default template file – "single-default.php")

Put this code in "single.php" as follows:

<?php
$post = $wp_query->post;
if ( in_category('2') )
{
include(TEMPLATEPATH . '/single-blog.php');
}
elseif ( in_category('3') )
{
include(TEMPLATEPATH . '/single-articles.php');
}
elseif ( in_category('4') )
{
include(TEMPLATEPATH . '/single-news.php');
}
else
{
include(TEMPLATEPATH . '/single-default.php');
}
?>
//In this code 2 is Blog Category ID , 3 is articles Category ID and 4 is News Category ID.

No Comments

Add your review

Your email address will not be published.

* Feilds are Required.