WordPress.org

Ready to get started?Download WordPress

Forums

How to change single post template based on category? (3 posts)

  1. nwatts88
    Member
    Posted 2 years ago #

    Hi, I've done a lot of searching on these forums and google, but can't figure this out. If someone could lend a hand I would be very appreciative!

    I've almost finished making a theme for my own blog that I've patched together from the Toolbox by Automatic (http://wordpress.org/extend/themes/toolbox). I'm doing this on my own computer via MAMP, so unfortunately I can't link anyone to it. I'm very new to this whole thing, I can find my way around CSS without too many emergencies, but with PHP I'm a bit hopeless.

    I would like to find a way to alter the single post template just for posts in one specific category. I have already set up a custom category archive page, which is the same as the rest of my theme just with a different header, but when I click posts within this category, the single post template kicks back in and presents the posts with the normal header.

    Would anyone know which extra bits and pieces (on the PHP side of things) I'd need to drop in to get this happening? Is this something that can be mended with a custom post type?

    Thanks so much!

  2. nwatts88
    Member
    Posted 2 years ago #

  3. dwood7399
    Member
    Posted 1 year ago #

    For the sake of anyone else looking, add this filter to your functions.php and name your template file to match the category slug: single-[cat slug].php. If you're using a child theme, replace TEMPLATEPATH with STYLESHEETPATH
    http://designisphilosophy.com/tutorials/creating-custom-single-post-templates/

    //Gets post cat slug and looks for single-[cat slug].php and applies it
    add_filter('single_template', create_function(
    	'$the_template',
    	'foreach( (array) get_the_category() as $cat ) {
    		if ( file_exists(TEMPLATEPATH . "/single-{$cat->slug}.php") )
    		return TEMPLATEPATH . "/single-{$cat->slug}.php"; }
    	return $the_template;' )
    );

Topic Closed

This topic has been closed to new replies.

About this Topic