1

Wordpress let me create custom tag templates, i have lots of tags, and i want to display the post tagged with some tags with a template, and the post tagged with other tags with another template. enter image description here

As this image shows the template hierarchy let me create the tag template easily, giving to the page the name or the id of the tag. But i want to create 1 template tag for a great number of tags (instead of making 1 template for every different tag), and another template tag for the remaining one.

4
  • how do you differentiate which template a particular tag receives?
    – Milo
    Mar 29, 2014 at 19:59
  • In wordpress you do this using as the title of the template the id of the tag or its slug. If the tag id is 3 the template will be tag-3.php I would like to attribuite the same template to multiple tags. But i don't really know how.
    – Benjamin
    Mar 29, 2014 at 20:10
  • you can force a particular template for multiple tags via the tag_template filter, but you have to decide how you will store the information regarding which tag gets which template, as there is no taxonomy metadata built into core. perhaps you could use the description field for this, or an option.
    – Milo
    Mar 29, 2014 at 20:23
  • @Milo could you please explain me more of this idea with maybe some code to try or more documentation on how to achieve this result?
    – Benjamin
    Mar 29, 2014 at 21:09

2 Answers 2

1

Here's an example that "abuses" the tag description field to store a template name for the tag. We use the tag_template filter to get the tag description if it exists and include a template file with the description as part of the name. You could extend this to whitelist specific template names so typos don't result in trying to load a file that doesn't exist:

function wpd_tag_template( $templates = '' ){
    $tag = get_queried_object();
    if( !empty( $tag->description ) ){
        $templates = locate_template( 'custom-tag-' . $tag->description . '.php', false );
    }
    return $templates;
}
add_filter( 'tag_template', 'wpd_tag_template' );

So for example the description contains template-1, WordPress will load the file custom-tag-template-1.php for this tag.

2
  • That's really a great idea. I'm testing it right now! I will tell you if it worked! Thanks
    – Benjamin
    Mar 30, 2014 at 13:24
  • The Final solution: If you have a great number of Tags, and want to create different templates for all these tags, but you want to use the same template for an indifferent number of tags, just use the field description, create the template, put this code in functions.php and it will work great! Thanks @Milo You did a great job in figuring out a solution with few lines of code and hussle-free!.
    – Benjamin
    Apr 1, 2014 at 12:03
2

You can use tag.php for the great number of tags and use tag-{slug}.php for remaining ones.

Also, you can use conditional tags for all in one file, tag.php:

if ( is_tag( '{tag-one}' ) ) {
  // only for tag with slug "tag-one"
} elseif ( is_tag( '{tag two}' ) ) {
  // only for tag with slug "tag-two"
} elseif ( is_tag( '{tag three}' ) ) {
  // only for tag with slug "tag-three"
} else {
  // Load default view for a great number of tags
}

Even, is_tag() accepts an array of slugs or ids.

I hope I get it right.

4
  • That's not the point. I would like to attribuite the same template to multiple tags chosen by me. The code you have posted will let me do that, i think because i haven't tested it yet, but it is not a great solution because it will create a file really really big. And will be the same result as creating 1 file for every tag.
    – Benjamin
    Mar 29, 2014 at 21:03
  • Oooooh, i don't know, so how many tags do you have? Maybe it's beter to ask... how many groups of tags do you have...? Mar 29, 2014 at 21:50
  • 3 groups of tags
    – Benjamin
    Mar 30, 2014 at 13:11
  • 3 groups, not so much :D Mar 30, 2014 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.