Check if entry has particular category

Random stuff about serendipity. Discussion, Questions, Paraphernalia.
Post Reply
Christine
Regular
Posts: 54
Joined: Wed Mar 25, 2009 11:50 pm

Check if entry has particular category

Post by Christine »

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)?
yellowled
Regular
Posts: 7111
Joined: Fri Jan 13, 2006 11:46 am
Location: Eutin, Germany
Contact:

Re: Check if entry has particular category

Post by yellowled »

Christine wrote:I'm hoping to add a prefix to the title of entries that are assigned to a 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 with

Code: Select all

foreach from=$entry.categories item="entry_category"}…{/foreach}
Within that loop, you have access to various category-related info like for instance the name:

Code: Select all

{$entry_category.category_name|escape}
See for example 2k11's entries.tpl for further data that can be obtained about categories. (As to why there's “|escape”, see https://www.smarty.net/docs/en/language ... escape.tpl.)

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
Christine
Regular
Posts: 54
Joined: Wed Mar 25, 2009 11:50 pm

Re: Check if entry has particular category

Post by Christine »

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.
yellowled
Regular
Posts: 7111
Joined: Fri Jan 13, 2006 11:46 am
Location: Eutin, Germany
Contact:

Re: Check if entry has particular category

Post by yellowled »

Christine wrote:But I don't understand where/how I specify a category name.
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.)

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>
You want to test if this entries category equals ”foo”. Translated to Smarty, that should look like this:

Code: Select all

<h2>{if $entry_category.category_name|escape == 'foo'}[foo]{/if} <a href="{$entry.link}">{$entry.title}</a></h2>
Please note that this is completely untested since I unfortunately don't have a test blog at the moment. It may or may not work, I can't test it right now. But I hope it works (and I hope I did understand correctly what you're trying to do).

YL
Christine
Regular
Posts: 54
Joined: Wed Mar 25, 2009 11:50 pm

Re: Check if entry has particular category

Post by Christine »

Thank you!!! I can confirm it works perfectly. Really appreciate your help :) .
Post Reply