Custom single-content layout?

Hey there,
I wonder if it’s possible to have different layouts for the single-content (posts) ?

I am trying to create a 2 colum layout for one specific category. One colum should be filled over custom fields, the with the content() .

Okay, I found a solution.

If you don’t want to rely on the plugin, change single.php to:

<?php get_template_part('templates/content-single', get_the_category()); ?>

This will then load templates/content-single-category-slug.php if it exists.

6 Likes

Hi, this is a nice workaround, I’ve tried this solution with no success… guess it should be like this:

<?php $cat = get_the_category(); get_template_part('templates/content-single', $cat[0]->slug); ?>

Y think get_the_category() returns an array of categories (see get_the_category in docs).

Thanks for adding your code.

Most of what I post is off the top of my head; I don’t always get it right but it should provide a gentle shove in the right direction. In practice you’d also probably want to iterate over the categories rather than only match the first.

There are a couple of trac issues that would help this but they’re old, so I won’t be holding my breath!

Hi, if anyone needs to add a custom layout of a single post depending on the category you can use also a filter.
I found this code:

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;’ )
);

This is useful if you have sub categories and want to use parents slug on single-[catname].php
I don’t know if it has performance differences.

Thank you, I was looking a workaround for this.