Support » Themes and Templates » Single.php & Archive.php based on Category

  • Is there any way to customize the template layout for the Single.php and Archive.php pages based on what category it is?

    I have custom templates made for each category page (category-1.php, etc) and I would like the layout to carry over to the individual entry and monthly archives.

    Any ideas?

    Thanks!

Viewing 10 replies - 1 through 10 (of 10 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    Not as easily, no. The reason for this is:
    -Single posts can belong to multiple categories, so which category template should it use?
    -Archive pages can have multiple posts, so again, which category template should it use?

    That said, it would be possible to roll your own code to deal with this sort of thing.

    All templates get run through a filter. In your case, the filters you’re interested in are single_template and archive_template. You could do something like this:

    function choose_single_template($template)
    {
    // $template contains the filename of the template being used (like "/single.php")
    // decide what template you want to use
    $template = "/single-whatever.php";
    // send it back
    return $template;
    }
    add_filter('single_template','choose_single_template');

    Basically, the filter gets the filename of the template. You can change it and return it and thus force a different template. You would probably want to put this sort of code in your theme’s functions.php file.

    You could probably do it with conditional statements

    Also read here:
    http://wordpress.org/support/topic/27073?replies=29

    For the single:
    http://guff.szub.net/2005/07/21/post-templates-by-category/

    With the monthly archives is more complicated because WP doesn’t filter posts by month AND category.

    Thanks!

    I believe this is what I’m looking for, there will be only one category per post.

    Now I just need to wrap my brain around it –

    this filter would be declared in template-loader.php?

    How would I pass the category? Maybe something like this:

    } else if ( is_single() && $template = get_single_template($mcat) ) {
    if ( is_attachment() )
    add_filter('the_content', 'prepend_attachment');
    include($template);
    exit; }

    function get_single_template($catid) {
    $template = TEMPLATEPATH . '/single-'$catid.php;
    return $template;
    }

    Thanks, blepoxp – I thought about using one big giant conditional for each of the pages (it’s my backup solution).

    moshu – I’ll have to take a look at that and digest it a bit, thanks!.

    That’s a plugin, you don’t have to “digest” anything – just install it and your single posts will work with the category templates.
    There is also another plugin mentioned on that page: the
    “inheritor” 🙂

    Yep, already working! 🙂 I’ll check out the inheritor – thanks!!

    No, Inheritor doesn’t help at all. I’m just going to pass the cat id via the url string and do some conditional statements.

    Thanks everyone!

    Hey, that Plugin works perfectly here, thanks Moshu

    One question I have, how would you change the title on the category template so it would change when you are in the archive section to “Archives” or something like that?

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Single.php & Archive.php based on Category’ is closed to new replies.