WordPress.org

Ready to get started?Download WordPress

Forums

HOWTO: Use a special template for certain categories (7 posts)

  1. vtxyzzy
    Member
    Posted 4 years ago #

    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');
    }
    ?>
  2. MichaelH
    Member
    Posted 4 years ago #

    Just for info, also see Template_Hierarchy#Category_display.

  3. acousticdryad
    Member
    Posted 4 years ago #

    Does this work for pages as well?

  4. vtxyzzy
    Member
    Posted 4 years ago #

    Pages don't have categories, but you can assign a unique template to a page in the Edit Page screen.

  5. acousticdryad
    Member
    Posted 4 years ago #

    Excellent, thank you vtxyzzy, that solves most of my problem. The only thing I've noticed doing it this way, is it seems to create an imaginary "post" at the top of the page, see here:

    http://aplusbag.com/blog/

    I am stvwlf's using method mentioned here to display only blogs from a certain category on this page:
    http://wordpress.org/support/topic/264482?replies=21

    If I can get the first entry (that's not really a blog) to disappear, then I am golden.

  6. vtxyzzy
    Member
    Posted 4 years ago #

    This is mostly a guess, but go to Posts->Edit and look for a post #163 with no title.

    I looked at the source for the page and saw that the href for that entry looked like this:

    href="http://aplusbag.com/blog/#respond".

    All the others looked something like:

    href="http://aplusbag.com/blog/test4/#respond".

  7. acousticdryad
    Member
    Posted 4 years ago #

    It looks like that is the actual page itself that these posts are on. I think at least for a temporary band-aid, I'll put a display: none attribute on the id for that post in the css.

Topic Closed

This topic has been closed to new replies.

About this Topic