Using More Than One Single Post Page

Yükleniyor

grey Birden Fazla Single.php Kullanımı

Single.php is a theme file that contains post contents. We can use more than one single.php to show different post templates depends on post’s category, tag or custom fields.

This article is a English version of this post: Birden Fazla Single.php Kullanımı.

Problem

  • I want to display posts from category ‘gallery’ in different template.
  • I wanna display posts that have custom field: ‘video_id’ in different post template.
  • I want no sidebar in posts from category number 13.
  • And so on.

Solution

Solution is pretty simple. If you know if/elseif/else statement logic, you can do it like a boss.

Before all our work, backup your theme files.

Backup is finished? Ok, let’s get it started.

  1. First, copy your single.php file and rename it to single-default.php.
  2. Then, re-copy your single.php file and rename it to single.xxx.php. Change xxx to what you want.
  3. And open your single.php file (original file, don’t open a copy) and remove all code and replace it:
    <?php if ( have_posts() ) {
          the_post(); rewind_posts();
     }
         if ( in_category(x)) {
             include(TEMPLATEPATH . '/single-xxx.php');
         } else {
             include(TEMPLATEPATH . '/single-default.php');
         }
     ?>
    
  4. Got it? Just change x to a category id if you want to show category based single.php. Or use get_post_meta($post->ID, 'xxx', true) in if statement for showing posts that have a custom field named ‘xxx’.
  5. After saving single.php file, just edit your copied single.php file named single-xxx.php (or what you renamed it).

Conclusion

This easy-to-code WordPress trick might guide you to create magnificent blog, daily news etc. websites. By the way, this one is my third English article. So, I had grammer mistakes or misspellings. If you catch a mistake or a better idea to solve content based single post pages, please write a comment.

Using More Than One Single Post Page” için 1 yorum

Bir Cevap Yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir