Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

My current work required me to assign a specific stylesheet/appearance for only one category of blog posts in a WordPress blog. By default, all blog post (single page) will use the single.php to display the post. There are a lot of ways of doing this, but I wanted it to be fast and simple.

This is my single.php code:

get_header();
if(have_posts()){
    while(have_posts()) {
        the_post();
        $id=get_the_ID();
        $cat=get_the_category();
        $postTitle=get_the_title();
        $post_type=get_post_type( $post );
        $subtitle='';
        $slider='none';
        $layout=get_opt('_blog_layout');
        $sidebar=get_opt('_blog_sidebar');      
        include(TEMPLATEPATH . '/includes/page-header.php');         
        ?>

        <div id="content-container" class="content-gradient <?php echo $layoutclass; ?> ">
        <div id="<?php echo $content_id; ?>">
        <!--content-->
        <?php

        include(TEMPLATEPATH . '/includes/post-template.php');

    }
} ?>

<div id="comments">
    <?php comments_template(); ?>
</div>
</div>

<?php 
if($layout!='full'){
    print_sidebar($sidebar); 
}

?>
<div class="clear"></div>
</div>
<?php get_footer();   ?>

I try put something like this in the single.php but i still cannot get what i want

<?php
if ( in_category('3') ) {
include(TEMPLATEPATH . '/single2.php');
} 
?>

How can i achieve this?

share|improve this question
Aha! I solved it! – Dzul Dec 20 '11 at 9:56
please post your answer and accept it. – mrwweb May 15 '12 at 17:34

closed as too localized by toscho Feb 19 at 0:01

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

2 Answers

Create Desired style for your different single page then on single.php you have to do this check, so desired single page will load for specific category type. And don't forget to give defult single as fall back.

$post = $wp_query->post;

if (in_category('1')) {

  include(TEMPLATEPATH.'/single1.php');

} elseif (in_category('2')) {

  include(TEMPLATEPATH.'/single2.php');

} else {

include(TEMPLATEPATH.'/single_default.php');

}

share|improve this answer
1  
Use get_template_part instead of include. – Joseph Dec 20 '11 at 15:01

Another way to approach this would be to echo the category slug as a class on the BODY tag, and create a set of styles dependent on that. This could get cumbersome if the special version of the page is very different from the default version, but if you're only making a few changes, it's low-overhead and gives you an easy hook for further customization.

share|improve this answer
If you're using the body_class() function, the category should get automatically added to the body. – mrwweb May 15 '12 at 17:34

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