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:

                    entry_id="not 3870"
                        <li class="{title}"> 
                            <div class="nav_dropdown">

                              <div id="sport_group" class="{title}-width">  

                                    {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>

  • 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. Mar 12 '15 at 14:32

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:

    {if {categories}1{/categories}=='1'}

        entry has 1 category


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


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; ?>
    <?php $count++; ?>
    This is the <?php echo $count; ?> category for this entry.

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.

  • 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. 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. 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.