2

Basic functionality I'm looking for has to do with counting the results from a reverse_related_entries pair while excluding certain categories. I'm hitting a wall. We have multiple locations and each location has their own audio and video files which are related to a specific subject called "series." Each location is a category and I only want to show AND count entries in one category and related to the series.

{exp:channel:entries channel="sermon_series" limit="1" dynamic="no"}

<!-- display some information about the series -->
<h1>{title}</h1>
{sermon_series_description}

<hr>

<!-- call up any child of the sermon series, sort them by date ascending -->
{reverse_related_entries channel="sermons" orderby="date" sort="asc" dynamic="off"}
    {if '{categories}{category_id}{/categories}'== "1915"}

        <!-- display some info about each sermon -->
        <h2>Part {count} - {title}</h2><br>
        <p><a href="{sermon_notes}">Sermon Notes</a></p>

        <hr>
    {/if}
{/reverse_related_entries}{/exp:channel:entries}

You can see what I'm trying to accomplish here: http://dev.ncctk.com/media/sermons/this-little-light-of-mine. You'll notice the {count} variable produces 1, 4, and 5. I've tried using a PHP increment as well, but the same thing happens as it loops through even the entries that don't meet the conditional.

2

Well, {count} is working as intended. It counts the entries returned by the tag - not entries within your conditional statement.

Unfortunately, {reverse_related_entries} is very limited in how it can be used (e.g., no category filter) - ostensibly for performance reasons.

Doing a PHP count in your template should work, so long as PHP is set to output and you increment your variable within your conditional statement. But a good alternative here is to use Playa instead of a stock Relationship field. Then you can use {exp:playa:parents}, which allows all the filtering you can normally apply to a Channel Entries tag (e.g., category="1915").

| improve this answer | |
  • So out of curiosity, is there a better way to do this with out a module? I already have tag in my arsenal so I could use that as well – kjellandrew Jan 31 '13 at 2:16
  • Sorry, Derek. Looks like I echoed two of your points. +1 for Playa. – Jim Butts Jan 31 '13 at 2:41
  • Jim's embed idea would be the way to go if you don't want to use Playa. – Derek Hogue Feb 1 '13 at 13:25
1

I pretty much exclusively use Pixel & Tonic's Playa addon for relationships. It's capable of handling more parameters than the reverse_related_entries tag. I would recommend this method since it does not require you to filter through an unknown number of incorrect results to get what you're looking for.

However, it's not the only way to accomplish what you're trying to do. The problem is that the count tag doesn't care if it's wrapped in a conditional, so it's also counting the entries that don't match your category criteria.

You could try Stash's set_list and get_list to build the results you need. I believe get_list's count variable would then be accurate.

As a last resort, I suppose you could also try a single embed (with some performance tradeoff and ugly code). Something like this untested example:

{embed="your_embed" entry_id='{reverse_related_entries channel="sermons" orderby="date" sort="asc" dynamic="off"}{categories}{if category_id == "1915"}{entry_id}|{/if}{/categories}{/reverse_related_entries}'}

Then, in your_embed, get {embed:entry_id} and put it in an entries tag's entry_id parameter.

When you tried the PHP increment, was it still incorrect when incrementing it inside the conditional?

tl;dr (in order of preference): 1. Playa 2. Stash's set_list, get_list 3. Enabling PHP in a template 4. embed

| improve this answer | |
  • Thanks. The PHP method was still a no go. I guess I could have wrapped it in another conditional. – kjellandrew Jan 31 '13 at 3:25
  • Even if you do the conditional in PHP? {categories} <?php if ("{category_id}" == "1915"){ echo $count++; } ?> {/categories} – Jim Butts Jan 31 '13 at 4:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.