0

I have a client who wants to create an alphabetical listing of his categories, broken out by letter. The only way I can think to do this is by restricting each list by the first letter of the category name, but I'm not sure how to accomplish that with expression engine parameters.

This is what I am trying to use, and it is not giving me any entries, even though I have entries whose category name starts with that letter.

<h4 class="panel-title">A</h4>
    {exp:channel:categories channel="products" style="linear" category_group="not 4"}
            {if category_name ^= 'a'}
            <ul>
                            <li><a href="{path=visas}">{category_name}</a></li>
            </ul>
            {/if}   
    {/exp:channel:categories}

Can someone tell me what I'm doing wrong?

0

There isn't something built in to EE, but as usual, there is a plugin to the rescue:

https://devot-ee.com/add-ons/gwcode-categories

http://gwcode.com/add-ons/gwcode-categories/documentation

It's basically a beefed up version of the {exp:channel:categories} tag. To get a result closer to what you want, you probably need to turn PHP on output and do something like this:

<?php $alpha_array = array (); ?>

<ul class="category_list">    
{exp:gwcode_categories orderby="cat_name"}

    <?php
        $cat = "{cat_name}";
        $starts_with = strtoupper($cat[0]);
        if (!in_array($starts_with, $alpha_array))
        {
            // if we haven't hit this letter yet, add it to our 
            // alpha array, and then output that letters header
            $alpha_array[] = $starts_with;
            ?>
            <li class="letter_header">
                <?=$starts_with?>
            </li>
            <li>
                {cat_name}
            </li>
            <?php
        }
        else
        {
            ?>
            <li>
                {cat_name}
            </li>
            <?php
        }
        ?>
    ?>

{/exp:gwcode_categories}
</ul>

This will output your categories, and then you can style the li tags with the letter_header class to look like h4 tags if you want.

ps do you have categories that start with a lowercase "a"...? I'd bet not.

5
  • 1
    What about LowAlphabet (also on Devot:ee)? That was something I saw mentioned on another thread elsewhere. Didn't seem like that one would require PHP coding in my templates. And, yes, I caught the lowercase "a" - changing it to uppercase didn't help. Thanks! May 29 '15 at 2:10
  • LowAlphabet is great for ordering channel entries, not categories. Unless you give designers or content editors access to your templates (super bad practice), turning on PHP parsing in a template is fine. It's easy enough to set your categories to be in alphabetical order, but doing the alpha header on each letter annoyingly requires the solution I pasted. I encourage other developers to chime in if I'm missing something obvious.
    – jrothafer
    May 29 '15 at 12:20
  • I turned "allow PHP" to "on" for my test template, but the example you gave is now producing a PHP error: "Parse error: syntax error, unexpected '{' in /home/jmgasia/ee/expressionengine/libraries/Functions.php(689) : eval()'d code on line 130" Jun 11 '15 at 12:51
  • There's actually nothing on line 130 in that file, nor anything about eval()'d, so I don't see the cause of the problem. Jun 11 '15 at 13:03
  • I edited my answer, was missing a ")" after the PHP if statement, but this is untested code, so don't be surprised if it needs tweaking by a developer to get working.
    – jrothafer
    Jun 11 '15 at 13:35

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.