Check if entry has particular category

Random stuff about serendipity. Discussion, Questions, Paraphernalia.
Post Reply
Christine
Regular
Posts: 51
Joined: Thu Mar 26, 2009 12:50 am

Check if entry has particular category

Post by Christine » Mon Aug 21, 2017 2:05 am

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)?

User avatar
yellowled
Regular
Posts: 6924
Joined: Fri Jan 13, 2006 12:46 pm
Location: Eutin, Germany
Contact:

Re: Check if entry has particular category

Post by yellowled » Mon Aug 21, 2017 8:31 pm

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
amazon Wishlist - Serendipity-Podcast (German only, sorry)

Christine
Regular
Posts: 51
Joined: Thu Mar 26, 2009 12:50 am

Re: Check if entry has particular category

Post by Christine » Tue Aug 22, 2017 4:18 am

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.

User avatar
yellowled
Regular
Posts: 6924
Joined: Fri Jan 13, 2006 12:46 pm
Location: Eutin, Germany
Contact:

Re: Check if entry has particular category

Post by yellowled » Wed Aug 23, 2017 9:34 pm

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
amazon Wishlist - Serendipity-Podcast (German only, sorry)

Christine
Regular
Posts: 51
Joined: Thu Mar 26, 2009 12:50 am

Re: Check if entry has particular category

Post by Christine » Thu Aug 24, 2017 3:51 am

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

Post Reply