• Resolved deepbevel

    (@deepbevel)


    I’m wondering if it’s possible to make all posts in a category, or with a specific tag, use a specific posts template by default.

    I have front end posting, all posts to one category, but how can I make these posts use a certain template?

    would “if” statements in the default single.php template work? and can it be done with tags as well as categories?

Viewing 12 replies - 1 through 12 (of 12 total)
  • Hi, deepbevel—

    Yes, you can do this with an easy if statement in single.php. Something like this inside the Loop should work:

    if ( in_category( 'your_category' ) ) {
        [ your markup... ]
    } else...

    ( Codex reference: http://codex.wordpress.org/Function_Reference/in_category )

    Thread Starter deepbevel

    (@deepbevel)

    Thanks, what about using a specific tag as the condition for using a specific post template?

    So if I had a tag called “large”, any post with that tag would use post template #2.?

    I’m havibg trouble finding info, it all relates to getting tags from posts, or “template tags”. “checking for a tags page”, or “if has tags” ..I’m coming up dry, starting to think it can’t be done without a new function…

    There’s a conditional check for post tags as well: has_tag().

    You’d use it in the same way as above. I’ve never used it to direct posts to specific templates, so I’m not sure what problems you might run into with posts with multiple tags (or categories, for that matter). As always, test it like crazy. 🙂

    If you are inside the Loop, I’d suggest using has_tag() in conjunction with get_template_part() to load tag-specific single post templates.

    Thread Starter deepbevel

    (@deepbevel)

    Great. I’ll keep working at it. In this situation, posts will have one of a few tags, it will determin the “layout” (post template) , as selected by front end user, for all posts with that tag. But for any category.

    Thread Starter deepbevel

    (@deepbevel)

    Thanks esmi, hard to say if it’s in the loop, users will use a form (tdo mini) to add the post and assign the tag which gets the template. I’m having a hard time thinking about the loop in this situation since posts aren’t being called, but submitted.

    So how are you grabbing the post id for the display?

    Thread Starter deepbevel

    (@deepbevel)

    These particular posts are displayed in a tag archive and as single posts, as well as the main blog. So I guess they’re displayed in the loop, but I also have queries in the sidebar which output titles.

    Thread Starter deepbevel

    (@deepbevel)

    Is get_template_part for an entire template, or just part of it (sidebar, fooer, header)? I’ve use “include (TEMPLATEPATH.” before to get templates with conditionals, will that method not work here?

    Thread Starter deepbevel

    (@deepbevel)

    I have auto categories based on tags now, this code puts a post tagged with “tag 1” automaticcally in a category called “tag 1” (tag name doesn’t have to match category name) wonder if I’m on the right path?
    I only understand about half of what this is doing, it’s not the conditional code I’m used to:

    add_action( 'wp_insert_post', 'update_post_terms' );
    
    function update_post_terms( $post_id ) {
        if ( $parent = wp_is_post_revision( $post_id ) )
            $post_id = $parent;
        $post = get_post( $post_id );
        if ( $post->post_type != 'post' )
            return;
    
        // add a tag
        wp_set_post_terms( $post_id, 'tag 1', 'post_tag', true );
    
        // add a category
        $categories = wp_get_post_categories( $post_id );
        $newcat    = get_term_by( 'name', 'tag 1', 'category' );
        array_push( $categories, $newcat->term_id );
        wp_set_post_categories( $post_id, $categories );
    }

    I imagine th part that has to change is the add a category code, somehow has to assign a post teplate indstead..

    However, I should be able to combine it with (in the default post single template):

    $post = $wp_query->post;
    if (in_category(id for "tag 1" category)) {
    	include (TEMPLATEPATH.'/custom-single.php');
    return;
    } get_header(); ?>

    but it seems like the long way around. Still I’m happy to have a way to do it 🙂

    Thread Starter deepbevel

    (@deepbevel)

    stepping back a bit..

    I can’t seem to get it to work with more than one instance, so I can get:

    “tag 1” goes to “some specified category”

    “tag 2” goes to “some other specified category”

    ect.

    I’ve tried a bunch of things, but I’ve only worked with a few function mods before. So far I’ve only managed to crash functions and force all posts to take 2 tags. Any help appreciated:)

    Thread Starter deepbevel

    (@deepbevel)

    Solved, and without having to add a function to get a tag-based category, to get a tag-based post template. Obviously still trying to get the hang of this.

    at the very top of single.php:

    <?php
    $post = $wp_query->post;
    if (is_tag('your-tag')) {
    	include (TEMPLATEPATH.'/your-custom-post-template.php');
    	return;
    }
    
    get_header(); ?>
Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Post template based on category or tag?’ is closed to new replies.