November
4th
2007
So, oft-times, one wants to show a heading only if there are results after that heading. For instance, over to the left, I want to show the Tags heading only if there are tags assigned to the entry. Ultimately, what this means, is that we need two conditionals:
{if results} ... {/if}
{if no_results} ... {/if}
But as far as I can see, that doesn't yet exist. So, I delved into queries. Mind you, this is only the second query I've ever written. The problem with the query is that I needed to use count and it was counting from the weblog entries tag instead of from the query code; I think I can use an AS to get around this, but I forgot how, so I just embedded the template and passed the entry_id from the weblog entries tag. Thsi checks to see if there are any tags for a given entry, and shows information based on the results.
So, here is a simple test, you can find the basic results of my initial tests at Test output
The code:
The embedded template is abracadabra/tagtestembed and contains this:
{exp:query sql="SELECT tag_id FROM exp_tag_entries WHERE entry_id='{embed:check_id}'"}
{if count == "1"}
<b>Tags: </b>
{/if}
{if no_results}
<p>This entry has not been tagged yet.</p>
{/if}
{/exp:query}
The main code, for the weblog entries tag is in abracadabra/tag_test, and is like so. Tables were used to create a simple border to see what results belong to which entry:
<table style="border: 2px solid black;">
{exp:weblog:entries weblog="dem" status="open|closed" limit="10"}
<tr style="border: 2px solid black;">
<td style="border: 2px solid black;">
<b>Title:</b> {title}<br />
<p>{embed="abracadabra/tag_test_embed" check_id="{entry_id}"} {exp:tag:tags entry_id="{entry_id}" backspace="2"}
<a href="{path="dem/tags"}{websafe_tag}">{tag}</a>, {/exp:tag:tags}</p>
</td>
</tr>
{/exp:weblog:entries}
</table>
As you can see from the output linked above, it works quite well. It's not the best way to create a conditional, but it's a solution until such a solution exists internal to the module.
I probably over-complicated it, but it did force me to play with queries, so whatever, eh?
Thanks, lady! This was really helpful. Can I do something similar with categories? How would that string look? I tried {if categories} (I swear, I feel like I ask this question a billion times and I never remember!), but it said ‘No, thank you.’