[resolved] template redirect function for single php? (6 posts)

  1. denl
    Posted 4 years ago #

    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”);
    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!

  2. Rev. Voodoo
    Posted 4 years ago #


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

  3. denl
    Posted 4 years ago #

    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!

  4. Rev. Voodoo
    Posted 4 years ago #

    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:

    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
    get_template_part default single loop/display (or just includea/leave your standard single.php stuff here
  5. denl
    Posted 4 years ago #

    Thanks! Yes that's what I want to do. :-)

    thanks very much

  6. Rev. Voodoo
    Posted 4 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic