WordPress.org

Support

Support » Themes and Templates » [Resolved] template redirect function for single php?

[Resolved] template redirect function for single php?

  • I have a custom post type in my child theme (of 2011). It’s added by a plugin. The single items are displayed in single-product.php and that works very well. But my problem is I need to display 3 different types of product and I want 3 different single templates. I wondered if I could do something like check for a certain taxonomy like cats, dogs or mice and then send it to a different version of my single template, like single-product-dogs.php?

    I found this on another site, can’t remember where now, I’ve been looking for ages…

    function my_template_redirect()
    {
    global $wp;
    if ($wp->query_vars["taxonomy"] == “example”)
    {
    include(TEMPLATEPATH . “/taxonomy-example.php”);
    die();
    }}
    add_action(“template_redirect”, ‘my_template_redirect’);

    It looks like what I want to do but I’ve no idea where I should put it. Would this go in single-product.php or does it go in the functions file? And if it would work how do I add the other two choices to the function?

    I’d be gratefull for any ideas!

Viewing 5 replies - 1 through 5 (of 5 total)
  • http://codex.wordpress.org/Template_Hierarchy#Custom_Taxonomies_display

    Just follow the proper naming conventions for each term/tax, and your templates will be used automagically

    Thanks for the idea. They don’t work though, it all goes to single-product.php instead. Although I’m probably misunderstanding how it should be named!

    I’ve tried taxonomy-product-terms-dogs.php and that didn’t work but taxonomy-post_format-post-format-link.php is a bit confusing.

    If My tax is called product-terms, my term is dogs and my custom post type is called productitems can you show me how I’d name that?

    Sorry to be a bit slow!

    I believe I misunderstood you. The naming conventions work great for getting an archive/list of your custom tax posts, but not single view

    Have a look through here:
    http://wordpress.org/support/topic/single-post-template-for-custom-taxonomies?replies=4

    Basically you make an overall single.php, that doesn’t have the content display portion….

    Use a conditional if/else to detect the terms, and bring in differing code based on the term, with logic like

    if -> cats
    
    get_template_part cats loop/display
    
    elseif -> dogs
    
    get_template_part dogs loop/display
    
    else
    
    get_template_part default single loop/display (or just includea/leave your standard single.php stuff here
    
    endif

    Thanks! Yes that’s what I want to do. 🙂

    thanks very much

    Woo hoo! I’m glad that worked out for you!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[Resolved] template redirect function for single php?’ is closed to new replies.
Skip to toolbar