WordPress.org

Support

Support » Themes and Templates » Is it possible to have multiple templates for SINGLE posts?

Is it possible to have multiple templates for SINGLE posts?

  • I have posts with category “cars”, “vehicles”, “bikes” and want them to have separate SINGLE templates based on their category.

    – “cars” single posts will have blue background colors.
    – “vehicles” single posts will have red background colors.
    – “bikes” single posts will have green background colors.

    Is there a way to do that (separation)? Please help.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Try the Post Templates by Category plugin. I think it’ll do what you’re after.

    As for displaying a list of posts within a category you may want to dabble with Category_Templates (if you haven’t already, that is).

    If all you need is a changeup of color, if you stick the category into the class of a wrapper div you can create CSS rules to set colors (and background images and overall look and feel if you want). If you’re not really changing the page structure, try CSS first.

    eg:

    single.php

    <div id="theme-wrapper" class="<?php the_category(' '); ?>">
    ...
    <h2>A big ol Buick</h2>
    ...
    </div>

    style.css

    /* colors */
    #theme-wrapper.cars h2 {
       background-color: blue;
    }
    #theme-wrapper.vehicles h2 {
       background-color: red;
    }
    #theme-wrapper.bikes h2 {
       background-color: green;
    }

    If that’s not what you need, you can use conditional tags in your single.php to make minor changes without having to worry about keeping three files in relative sync. http://codex.wordpress.org/Template_Tags/in_category

    (There are plenty of features in WordPress to accomplish things without adding too much complexity.)

    I already have the category template issue figured out. But having separate templates for single posts off those categories are the main problem, and it’s beyond just changing color. Each template will have completely different layout, images and feel to it.

    Did you look at the plugin I linked yet?

    Moderator Samuel Wood (Otto)

    @otto42

    WordPress.org Tech Dude

    Something like this in single.php:

    <?php
    if ( have_posts() ) { the_post(); rewind_posts(); }
    if (in_category(1)) {
    include 'single_cars.php';
    }
    else if (in_category(2)) {
    include 'single_bikes.php';
    }
    else if (in_category(3)) {
    include 'single_vehicles.php';
    }
    ?>

    Did you look at the plugin I linked yet?

    Working great. Thanks 🙂

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Is it possible to have multiple templates for SINGLE posts?’ is closed to new replies.