• Resolved vavroom

    (@vavroom)


    I need to be able to assign “tag templates” like we’re able to assign “page templates”. Not sure if this is possible.

    I understand the template hierarchy, where tag-slug.php is looked for, the tag.php, etc. That’s fine. Except that I have hundreds of tags, and I need to display them in two or three different ways. It is not practicable to create a new tag-slug.php for each tag added. Is there a way to assign a tag template? You can do it for pages, but not tags that I can see…

    For example, if my tags are: labrador, poodle, persian, terrier, ragdoll, and siamese, and my templates are cats and dogs, I’d like to assign “cats” to persian, ragdoll and siamese, and “dogs” to labrador, poodle and terrier.

    Then, later, if I use the tags mastiff, shepherd and spaniel, I can just assign them the “dogs” template.

    Any assistance on this would be greatly appreciated. I haven’t seem to find anything on this topic despite searching the codex, the forum and googling it.

    The site is being developped locally at the moment, so no live link, sorry.

    Thanks

Viewing 8 replies - 1 through 8 (of 8 total)
  • You might be able to write an IF/THEN statement that lists your tag slugs in an array and you tell it if tag = this this this or this, use whateverfilename.php

    I have done this with categories but never with tags. The code looked something like this in category.php:

    <!-- START SPECIAL CATEGORY -->
    <?php if ( is_category('42')) { ?>
    include 'special-cat-template.php';
    <?php } ?>
    
    <!-- START 2nd SPECIAL CATEGORY -->
    <?php if ( is_category('50')) { ?>
    include 'special-cat2-template.php';
    <?php } ?>

    Also, to do them several at a time it would look like:

    <?php if( is_category(1) || is_category(4) || is_category(6) || is_category(20) || is_category(26) ) { ?>

    I just looked and is_tag is a supported condition and tags are assigned IDs so this **should** work in tag.php. You’ll just need to find the ID for all of your tags.

    Thread Starter vavroom

    (@vavroom)

    Thanks bekabug, I might have to resort to something like this, yes.

    I was hoping to avoid that approach, because at last count I have over 150 tags, and it’ll grow, too… So it will make for a rather long conditional statement.

    Unless someone has written a plugin (that I can’t find) or wants to write a plugin to achieve this with a GUI I don’t know of any other way.

    You could achieve this with categories instead of tags (but still using all your tags) since it sounds like you’ll have 3 maybe 4 of those to keep up with.

    Good luck

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    Actually, is_tag only takes slugs, not ID numbers. is_category can take id’s, names, or slugs.

    So it would be is_tag(‘poodle’) and so forth.

    A somewhat more clever way would be to use tag.php, start your Loop early and get the first post, and then use the has_tag() function to check for the tag/tags you want to check for. Then do a rewind_posts to back up the Loop before you fire off an include to tag-groupname.php, which is your group template.

    The reason to do this is because has_tag can take an array of tags and check all of them. Like so:

    if (has_tag(array('tag1','tag2','tag3')) {
    // do whatever
    }

    Ahhh…good to know. I don’t really do much with tags
    Thanks 🙂

    Thread Starter vavroom

    (@vavroom)

    Otto, thanks for that. I’ll try it.

    What do you mean by “start the Loop early” ?

    Thanks

    Thread Starter vavroom

    (@vavroom)

    Ah ha! I’ve done it now 🙂 It hadn’t occured to me to use category tags on a tag.php. But playing around a bit, that’s what I’m doing. All the ones that I want to single out belong to a specific category already.

    Sooo, here’s what I’m doing inside the loop:

    <?php if ( in_category('3') ): ?>
      something
    <?php   else : ?>
    something else
    <?php endif; ?>

    Using the assigned category avoids having an hyper-long array, and means I don’t have to edit the tag.php all the time I add new stuff 🙂

    Thanks to all who helped 🙂

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Assigning templates to grouping of tags?’ is closed to new replies.