Check if entry has particular category
Check if entry has particular category
I'm hoping to add a prefix to the title of entries that are assigned to a particular category. I have a separate template for this category, so it's no problem adding this to the category page or in a single entry view. But I'd like the prefix text to also show up in the archives/search results in the main template. Is there a variable that I can use to tell it: - if the entry belongs to a particular category, do (whatever)?
Re: Check if entry has particular category
Not sure if it's a problem, but it's worth noting that an entry may be assigned to multiple categories. You can, however, iterate over all possible categories in Smarty templates withChristine wrote:I'm hoping to add a prefix to the title of entries that are assigned to a particular category.
Code: Select all
foreach from=$entry.categories item="entry_category"}…{/foreach}
Code: Select all
{$entry_category.category_name|escape}
There's a small caveat. Like all Smarty variables, these may or may not be available in any given template file. They are available in entries.tpl (that's where they're used, after all), but not in other template files. I can't explain very well why that's the case. (By the way, https://docs.s9y.org/docs/developers/themes.html can be a good resource for all things Smarty in s9y.)
Another problem, of course, is that you need to know the category names you want to address while writing the Smarty code for this, but that should not be an issue in your case.
YL
Re: Check if entry has particular category
Sorry, I'm trying my best to learn as I go, but I'm a little lost here. I'm looking at examples of the code you gave me in my template and think I understand how it works. But I don't understand where/how I specify a category name.
If it helps, every entry in this category strictly only has this category.
If it helps, every entry in this category strictly only has this category.
Re: Check if entry has particular category
Well, you said you wanted to “add a prefix to the title of entries that are assigned to a particular category”. That means you need to test if any given entry is assigned to said category, i.e. if the category name of that entry equals the name of the category you're looking for. (This is where assigning multiple categories to entries could make things complicated.)Christine wrote:But I don't understand where/how I specify a category name.
So, for the sake of demonstrating it, we'll assume you're looking for entries in the category named “foo”, and you want to prefix the title of those entries with “[foo]”. Let's assume you're doing that in 2k11. The entry title is usually emitted in the file entries.tpl, in 2k11 this looks like this:
Code: Select all
<h2><a href="{$entry.link}">{$entry.title}</a></h2>
Code: Select all
<h2>{if $entry_category.category_name|escape == 'foo'}[foo]{/if} <a href="{$entry.link}">{$entry.title}</a></h2>
YL
Re: Check if entry has particular category
Thank you!!! I can confirm it works perfectly. Really appreciate your help .