Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
How to separate categories with a comma only when appropriate
The problem for me was that if I had two categories in a post I wanted them separated with a comma:
Filed under category1, category2
but if there were only one category, I wanted the comma omitted.
Filed under category1
I have been mulling this one over for a while, and since I couldn’t find a solution, I thought I would make a post. smd_if really made this simple, and as a bonus, if neither category is set, Filed under doesn’t even appear. The downside is that it doesn’t take into account if category2 is the only one set, but I decided that was a bad practice and I didn’t feel like writing extra code to support it. You can see examples of all three possibilities.
<txp:smd_if field="category1" operator="isused">
<txp:smd_if field="category2" operator="isused">
<txp:variable name="categories" value='<txp:category1 link="1" title="1" />, <txp:category2 link="1" title="1" />' />
<txp:else />
<txp:variable name="categories" value='<txp:category1 link="1" title="1" />' />
</txp:smd_if>
</txp:smd_if>
<p class="post-info">
Posted by <txp:author link="1" />
<txp:smd_if field="txpvar:categories" operator="isused">
| Filed under <txp:variable name="categories" />
</txp:smd_if>
</p>
Offline
Re: How to separate categories with a comma only when appropriate
Michael, you can do this also:
<a href="<txp:site_url />category/<txp:category1 />/"><txp:category1 /></a>
<txp:if_article_category number="2">, <a href="<txp:site_url />category/<txp:category2 />/"><txp:category2 /></a></txp:if_article_category>
We Love TXP . TXP Themes . TXP Tags . TXP Planet . TXP Make
Offline
Re: How to separate categories with a comma only when appropriate
I’ve not tested it, but in theory this should work too with whichever category is marked:
<txp:variable name="numcats" value='<txp:if_article_category number="1">-</txp:if_article_category /><txp:if_article_category number="2">-</txp:if_article_category />' />
<p class="post-info">
Posted by <txp:author link="1" />
<txp:if_variable name="numcats" value="">
<txp:else />
| Filed under <txp:category1 link="1" title="1" /><txp:if_variable name="numcats" value="--">, </txp:if_variable><txp:category2 link="1" title="1" />
</txp:if_variable>
</p>
Uses one assumption and a trick:
- Assumption:
txp:category1
/txp:category2
outputs nothing if no category is specified, so you get either one or the other if only one is set - Trick: You only show the comma if you know there are two categories to separate. The variable outputs a dash per category, so if that is the case the variable should have two dashes.
If it works, you can, of course, integrate the same assumption into your code…
TXP Builders – finely-crafted code, design and txp
Online
#4 2011-06-11 22:22:35
- els
- Moderator
- From: The Netherlands
- Registered: 2004-06-06
- Posts: 7,458
Re: How to separate categories with a comma only when appropriate
Hey, that’s a nice one, Jakob! I’d love to see this become part of the default article form, this question has been asked countless times.
Offline
Re: How to separate categories with a comma only when appropriate
As cool as smd_if is, I really prefer plugin-free solutions and now there are 2 to choose from. Thanks, guys. I was hoping posting that would get some feedback, but I didn’t expect quite this much.
Els wrote:
I’d love to see this become part of the default article form, this question has been asked countless times.
I completely agree. This is the sort of thing people would naturally expect to be able to do, but it isn’t particularly obvious. Quoting myself:
I don’t know that the default template has to be the be-all end-all, but I actually do think something that really shows off what the templating system can do and would actively work as a teaching tool might be a good choice.
Slightly addition: I can’t find countless times, but after looking some more, I did find one.
Last edited by michaelkpate (2011-06-12 01:51:13)
Offline
#6 2011-06-12 11:44:51
- els
- Moderator
- From: The Netherlands
- Registered: 2004-06-06
- Posts: 7,458
Offline
Re: How to separate categories with a comma only when appropriate
Els wrote:
The countless times must be in my memory then ;) but there are some more.
Ah, this forum, a wealth of information.
We Love TXP . TXP Themes . TXP Tags . TXP Planet . TXP Make
Offline
Re: How to separate categories with a comma only when appropriate
I don’t mind using Google for a search function – it is just a matter of guessing what keywords people use. My first thought was categories comma – most of the hits use those keywords, but have nothing to do with my original question.
The problem isn’t technology as much as psychology. :)
Offline
Re: How to separate categories with a comma only when appropriate
michaelkpate wrote:
The problem isn’t technology as much as psychology. :)
Yeah, it’s like mind reading ;)
I just want to bring up a point Stuart has said on this forum, that template tags allow you to do quite a bit, and you should exercise them before reaching for a plugin. I totally agree with him and I think he has this approach from years of creating templates.
When you create a template for mass consumption, you take a different approach than a designer would take to create a website for a client. A designer might be time constricted, where creating a template is a hobby of sorts and you can take your time and experiment.
We Love TXP . TXP Themes . TXP Tags . TXP Planet . TXP Make
Offline