WordPress.org

Ready to get started?Download WordPress

Forums

Single.php & Archive.php based on Category (11 posts)

  1. bcreighton
    Member
    Posted 7 years ago #

    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!

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    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.

  3. Glenn Ansley
    Member
    Posted 7 years ago #

    You could probably do it with conditional statements

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

  4. moshu
    Member
    Posted 7 years ago #

    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.

  5. bcreighton
    Member
    Posted 7 years ago #

    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;
    }

  6. bcreighton
    Member
    Posted 7 years ago #

    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!.

  7. moshu
    Member
    Posted 7 years ago #

    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" :)

  8. bcreighton
    Member
    Posted 7 years ago #

    Yep, already working! :) I'll check out the inheritor - thanks!!

  9. bcreighton
    Member
    Posted 7 years ago #

    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!

  10. desmondo
    Member
    Posted 7 years ago #

    Hey, that Plugin works perfectly here, thanks Moshu

  11. desmondo
    Member
    Posted 7 years ago #

    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?

Topic Closed

This topic has been closed to new replies.

About this Topic