WordPress.org

Support

Support » How-To and Troubleshooting » HOWTO: Use a special template for certain categories

HOWTO: Use a special template for certain categories

  • I wanted to use a different template for certain categories of posts, so I renamed single.php to single-default.php and created a new single.php to test the categories. If it is one of the special categories, use single-in-category.php, otherwise, use single-default.php.

    I originally hard-coded the categories in the new single.php, but as I added new ones, I had to edit the code. My solution was to use http://planetozh.com/blog/my-projects/wordpress-theme-toolkit-admin-menu/.

    This toolkit was easy to integrate with my theme; all I had to do was copy themetoolkit.php to my theme directory and add one function to functions.php. Then it was simple to add an option called single_in_category. This contains a comma separated list of the category slugs or IDs. Now I can add or remove a category in the Admin page.

    Here is my new single.php:

    <?php
    // single.php is now in single-default.php
    // We want to use single-in-category for
    // all categories listed in the single_in_category
    // option which is set in the Appearance->Miata Club Options panel.
    // Uses global $mclub created by themetoolkit in functions.php
    global $mclub;
    $post = $wp_query->post;
    $mycats = array();
    if ($mycatlist = $mclub->option['single_in_category']) {
      $mycats = explode(',',$mycatlist);
    }
    
    if ( in_category($mycats)) {
      include(TEMPLATEPATH . '/single-in-category.php'); }
    else {
      include(TEMPLATEPATH . '/single-default.php');
    }
    ?>
Viewing 6 replies - 1 through 6 (of 6 total)
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘HOWTO: Use a special template for certain categories’ is closed to new replies.