Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Display linklist category title if links exist
Having a small problem with a list of links separated by category title. What should happen is that the category title should only display if links exist within the category. Something like this:
cat1 title
link1
link2
cat2 title
link3
link4
….
But what happens instead is:
cat 1 title
cat 2 title
link 1
link 2
Its probably a small issue of having things the wrong way around, but for now its escaping me. I’ve tried if_different
and all sorts of variations for the variable
value. This is the code at the moment. This is probably something for Els to solve!
<txp:category_list type="link">
<txp:variable name="link_list" value="<txp:category type='link' />" />
<txp:if_variable name="link_list" value="">
<txp:else />
<h3><txp:category type="link" title="1" /></h3>
<div class="link_entry">
<txp:linklist category='<txp:category />'>
<p><txp:link /><br /><txp:link_description /></p>
</txp:linklist>
</div>
</txp:if_variable>
</txp:category_list>
Offline
Re: Display linklist category title if links exist
Hi
maybe problem of quote you must use simple quote here:
<txp:variable name="link_list" value='<txp:category type='link' />' />
Offline
Re: Display linklist category title if links exist
Hi Jonathan, how about changing this:
<txp:variable name="link_list" value="<txp:category type='link' />" />
into this:
<txp:variable name="link_list"><txp:linklist category='<txp:category />' limit="1" break="">yes</txp:linklist></txp:variable>
Does that help?
Otherwise, I would have thought your situation would be an ideal candidate for if_different
but then not using txp:category_list
. Something like this (untested):
<txp:linklist limit="999" break="">
<txp:if_different>
<h3><txp:category type="link" title="1" /></h3>
</txp:if_different>
<div class="link_entry">
<p><txp:link /><br /><txp:link_description /></p>
</div>
</txp:linklist>
TXP Builders – finely-crafted code, design and txp
Offline
Re: Display linklist category title if links exist
Thanks Julian and Rabah for the pointers. I’ve tried most of the variations already, including the first code example from Julian, but it didn’t work before. Maybe because I forgot to change to single quotes, who knows.
Julian, the <txp:link list />
example doesn’t work. Without investigating more, I don’t know why. I’ve tried a variation of your code before.
Here is what works for now (I’ll post it on TXP Tips for everyone’s benefit):
<txp:category_list type="link" break="">
<txp:variable name="link_list"><txp:linklist category='<txp:category />' /></txp:variable>
<txp:if_variable name="link_list" value="">
<txp:else />
<h3><txp:category type="link" title="1" /></h3>
<div class="link_entry">
<txp:linklist category='<txp:category />'>
<p><txp:link /><br /><txp:link_description /></p>
</txp:linklist>
</div>
</txp:if_variable>
</txp:category_list>
Offline
Re: Display linklist category title if links exist
And using link_category instead of txp:category and the sort attribute in linklist?
<txp:linklist limit="999" break="" sort="category asc, linksort asc">
<txp:if_different>
<h3><txp:link_category title="1" /></h3>
</txp:if_different>
<div class="link_entry">
<p><txp:link /><br /><txp:link_description /></p>
</div>
</txp:linklist>
Last edited by jakob (2011-09-03 14:11:07)
TXP Builders – finely-crafted code, design and txp
Offline
#6 2011-09-03 20:02:52
- els
- Moderator
- From: The Netherlands
- Registered: 2004-06-06
- Posts: 7,458
Re: Display linklist category title if links exist
Jonathan, can’t you adapt this TXP Tip for links instead of files?
Offline
Re: Display linklist category title if links exist
Els, I actually did play around with your suggestion, but for some reason or another it didn’t quite work out.
Julian, thanks for the code. I did not know link_category even existed! Not sure how I missed that, but makes me happy to discover something new about TXP!
Your code is much cleaner than mine, and that bothered me as much as anything else…I hate wasted code. Only problem is, every link is wrapped in the <div class="link_entry>"
instead of just the <p>
tag, so for now, I left my code as-is.
Offline
Re: Display linklist category title if links exist
Yes, I forgot about link_category too, hence the first example couldn’t work.
I did think of doing something like this:
<div>
<txp:linklist limit="999" break="" sort="category asc, linksort asc">
<txp:if_different>
</div>
<h3><txp:link_category title="1" /></h3>
<div class="link_entry">
</txp:if_different>
<p><txp:link /><br /><txp:link_description /></p>
</txp:linklist>
</div>
which gets what you want but has a stray blind div
at the top (no great problem but not 100% clean). Unfortunately links have no if_first/if_last mechanism. One approach to skipping the first item might be to set a variable after the first link that triggers the closing </div>
but if_different may see that as a ‘difference’ and not behave as desired (see the discussion in a parallel thread, also for a possible approach to a solution):
<txp:variable name="linklist_started" value="" />
<txp:linklist limit="999" break="" sort="category asc, linksort asc">
<txp:if_different>
<txp:if_variable name="linklist_started" value="yes"></div></if_variable>
<h3><txp:link_category title="1" /></h3>
<div class="link_entry">
</txp:if_different>
<p><txp:link /><br /><txp:link_description /></p>
<txp:variable name="linklist_started" value="yes" />
</txp:linklist>
</div>
If you have no luck with that or a version thereof inspired by Els ideas in the other thread, you might be able to construct an own if_first_link solution using smd_if.
TXP Builders – finely-crafted code, design and txp
Offline
Re: Display linklist category title if links exist
Thanks Julian, there may be other ways to achieve this goal, but as my code above works perfectly there is not much point in going further, unless a new solution is cleaner.
It is incredible how many ways there are to achieve things in TXP! I’m always amazed how you, Els and others always come up with variations.
Offline