0

On a single entry page, I want to display a list of categories an entry is assigned to, and these categories should link back to their respective pages. Now those categories are attached to different templates so I need to display the template name dynamically for each category (finance, corporate, real-estate, etc.)

I created this query:

{exp:query sql="SELECT template_name FROM exp_templates WHERE template_id = ‘{template_group}’”}{template_name}{/exp:query}

but unfortunately it still doesn’t display my template name.

To give you an idea, if I use this query:

{exp:query sql="SELECT group_name FROM exp_category_groups WHERE group_id = '{category_group}’”}{group_name}{/exp:query}

Then the group name shows correctly (Finance, Corporate, Real Estate, etc.), but I don’t need the group name, I need the template name!

Any suggestions to point to the right direction?

Thanks in advance!

  • How about putting the template name in a custom category field? – AllInOne Oct 12 '16 at 20:50
  • Could you please expand on this? I'm not sure I understand... – lando Oct 13 '16 at 8:21
1

Are the separate templates really different enough to warrant being separate? Usually you could code in the logic for slightly different views within a single template - much simpler and more maintainable than duplicating lots of code.

Anyway, assuming you want to keep them as separate templates, I can think of 3 options:

  1. As suggested by @AllInOne, specify which template should be used via a custom category field

  2. Use a naming convention ensuring that your category_url_titles match your template group names

  3. Use something like Template Routes (or third party Resource Router is even better!) to route certain URI requests to the right template.

Like I said, my personal preference would be to re-arrange things so that just a single dynamic template is used instead of lots of different ones, but it really depends how different they are from each other. (You would then feed that single template with a category name and take different actions depending on that).

| improve this answer | |
  • 1) and 2) aren't really applicable because Custom category fields and Category URL Titles don't apply to Category Groups. – lando Oct 13 '16 at 11:00
  • Now every time I hit Enter my comment goes live sorry! Anyway I am already using Resource Router but I can't get it to work properly for this specific problem I'm having. Thanks @AllInOne and James Smith for your help anyway! – lando Oct 13 '16 at 11:03
  • Actually just went ahead and used Custom Category Fields... and it works! Thanks both for your help! – lando Oct 13 '16 at 11:33
  • 1
    @lando why are the industry sectors in different category groups? – James Smith Oct 13 '16 at 13:07
  • 1
    Semantically, it sounds like those should not be category groups as they are all the same type of thing. Are you aware that you can nest categories in a parent-child hierarchy? Having them all in the same group would then allow you to use a naming convention. – James Smith Oct 13 '16 at 13:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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