WordPress.org

Ideas

category specific single.php templates

  1. Paul Menard
    Inactive

    Need a way to have a different single.php template file called based on category. This is similar to the category-x.php logic which already exists in WordPress core. But the category-x.php is used for multiple items not for a single post item.

    So my thought is the logic for category-x.php can be extended to support single-x.php where x is the category ID.

    An exmaple:

    I have two categories: Events(2) & News(3)

    I can create category-2.php and category-3.php to display the respective category listings. But to display the single post item from each category they would use single-2.php or single-3.php instead of the default single.php

    Posted: 11 years ago #
  2. Rongo
    Member

    12345

    +1 for this idea too.

    Posted: 11 years ago #
  3. yazerty
    Member

    12345

    already possible with a small little hack in index file :). But perhaps I didn't understand what you reqested :-?

    Posted: 11 years ago #
  4. Justin Tadlock
    Inactive

    12345
  5. johngrinde
    Member

    12345

    I found this while reading a forum posting. I've started to use it and it works very well. The code below will go into your functions.php file.

    <?php add_filter('single_template', create_function('$t', 'foreach( (array) get_the_category() as $cat ) { if ( file_exists(TEMPLATEPATH . "/single-{$cat->term_id}.php") ) return TEMPLATEPATH . "/single-{$cat->term_id}.php"; } return $t;' )); ?>

    Posted: 10 years ago #
  6. madog32
    Member

    Thanks,

    It works a treat!

    Posted: 10 years ago #
  7. Tote bag
    Member

    The code mentioned above by johngrinde works very well but if you are in the habit of filing posts under multiple categories, this code probably won’t work as intended, because it's working with templates for use on single posts in single categories, posts in multiple categories may end up falling back to the default single.php template. You have been warned!

    Posted: 9 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.

  • Rating

    12345
    11 Votes
  • Status

    This is plugin territory