1

I'm referring to EE docs on categories looping pair within channel entries tag. I'm looking for a count and total count param. Is there a way to pull this information out?

Here is the code -- I'd like to have a count within the wrapped {categories} {/categories} tag pair:

              {exp:channel:entries
                    channel="division"
                    orderby="title"
                    sort="asc"
                    entry_id="not 3870"
                    status="open|closed"
                    {global:disable_default}
                    }
                        <li class="{title}"> 
                            <a>{title}</a>
                            <div class="nav_dropdown">
                            {division_tagline}
                            <hr/>

                            {categories}
                              <div id="sport_group" class="{title}-width">  
                                <h3>{category_name}</h3>

                                    {exp:playa:children site="nxtsports" status="open|closed" field="division_brands" disable="" category="{category_id}"}
                                        <a href="{brand_url}" target="blank"><img class="brand_logo" src="{brand_logo}"></a>
                                    {/exp:playa:children}
                                </div>
                            {/categories}


                            </div>
                        </li>   
                {/exp:channel:entries}
2
  • Need more info: A total count of the categories assigned per entry? Or a total count of entries per category? – Cheyney Mar 11 '15 at 7:12
  • Just edited the post to give you a better idea -- thanks for chiming-in. – Colin Ambler Mar 12 '15 at 14:32
2

You can get a category count without using PHP using a if statement on a string built by the {categories} tag inside of the entry's exp:channel:entries tags. such as this:

{exp:channel:entries}
    {if {categories}1{/categories}=='1'}

        entry has 1 category

    {if:else}

        entry has multiple categories.
        {!-- i.e.: if '111' == entry has 3 categories  --}

    {/if}
{/exp:channel:entries}
0

Granted this is not the ideal way of getting the total_results and count in the categories tag pair, but it "works" and I've never have found an alternate solution. You have to enable PHP and set it to parse on output.

<?php $count = 0; ?>
{categories}
    <?php $count++; ?>
    This is the <?php echo $count; ?> category for this entry.
{/categories}

You can then use $count where ever you need to display it.

There are <?php echo $count; ?> categories for this entry. 

I would love to hear if someone had a better solution.

2
  • Thanks for response. Confirmed that this works! But I have two hiccups. 1) Template I'm using is a snippet that gets referenced in nearly every other template across a few msm sites. I'd have to enable php in several places, which would be a pain. Maybe an embed would work instead? 2) I want to know how many categories are part of the loop so that I can apply slightly different CSS layout for each scenario -- 1, 2, 3, 4 etc. It doesn't seem like I can get the total count before the loop. Is there a better way? My next option was jquery. – Colin Ambler Mar 12 '15 at 23:15
  • 1) Embeds will work a lot better, and I think they are cleaner anyway. You'll only have to enable PHP on that template. 2) You can always duplicate the {categories} tag pair and just put this above the other tag pair so you have the total count stored in $count. I know it's redundant, but if you're not too worried about optimization it should work fine. – BrandonOHara Mar 13 '15 at 17:02

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.