Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am smashing my head against some code. Here is the situation: I have one post in 3 different categories, this post has to be visible in "category 1" with the single1.php, in "category 2" with single2.php and in "category 3" with single3.php.

Obviously the 3 single.php pages have a different template inside. For example the single1.php shows pictures and the_content(); single2.php shows pictures and comments; single3.php shows the reviews.

I know that in single.php I can use the if/else but I cannot figure out if the same post is in 3 different categories.

Any help please?

share|improve this question
    
Mark, nice Question (+1) and two very good Answers down here. Upvoting is the StackExchange way to give "thanks". Also, you need to select one of the answers as the "correct" one, so this Question is not left as "unanswered". I would suggest choosing the one you ended up using, but up to you of course... –  brasofilo Oct 21 '12 at 12:37
    
Ye sorry, I hope there was a change for me to vote both as I can do on stackoverflow. Both were awesome. –  Mark Oct 21 '12 at 14:41
    
ups, I always forgot that you need a minimum rep to vote, in no time you'll be able to, regards! –  brasofilo Oct 21 '12 at 16:39
    
Ye ye I know ;) Just sad to not vote for both :( Regards! –  Mark Oct 21 '12 at 16:49

3 Answers 3

up vote 2 down vote accepted

Instead of making it category specific you could use post formats and use different content-templates. In single.php you can write

<?php get_template_part( 'content', get_post_format() ); ?>

Then create different post formats

add_theme_support( 'post-formats', array( 'withpictures', 'withcomments' ) );

Then create different post templates content-withpictures.php, content-withcomments.php

When creating content the chosen post format will determine the template.

share|improve this answer
    
Thank you for the answer. Really helpful –  Mark Oct 21 '12 at 10:04

You can use the filter hook for 'single_template'. Create single-cat1.php, single-cat2.php and single-cat3.php (cat1, cat2 and cat3 are the category names should be replaced by your category names)

function template_change( $template ){
    if( is_single() && in_category('cat1') ){
        $templates = array("single-cat1.php");
    } elseif( is_single() && in_category('cat2') ){
        $templates = array("single-cat2.php");
    } elseif( is_single() && in_category('cat3') ){
        $templates = array("single-cat3.php");
    }
    $template = locate_template( $templates );
    return $template;
}
add_filter( 'single_template', 'template_change' ); //'template_include'/'single_template'
share|improve this answer
    
Thank you for the answer. Really helpful –  Mark Oct 21 '12 at 10:05

You can change it on header.php if template using single-cat1.php, header.php must change style sheet Ex:

<link rel="stylesheet" type="text/css" href="<?php bloginfo('template_url'); ?>/ca1.css">
share|improve this answer
    
This is not the best way to include a stylesheet and please explain how that loads only on single-cat1.php and not on every page of the site? –  s_ha_dum Jul 18 '13 at 4:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.