1

I've searched the EE forum archives and have been unable to find a solution to a problem I'm having. I have a custom category field that I'm using to determine placement of category links throughout my website (e.g main nav, supporting links, etc). This is working fine, however, my issue is that I'd like to not write the ',' after the category link (see code snippet below) if its the last category to be displayed of those that are flagged to show. I can't use {count} and {total_results} vars here since I don't know up front which categories will need to be displayed (based on the custom field). If these were channel entries, I could use the search:field_name option to filter the entries and then use count and total_results vars, but that doesn't exist for the channel:categories tag. Any ideas of how to solve this one? I'm running EE 2.5.5

{exp:channel:categories style="linear" category_group="3"}
            {if nav_location == 'Display in primary and header navigation'}
                <a href="{path="{category_url_title}"}">{category_name}</a>,
            {/if}
{/exp:channel:categories}
2

If you were to output them as a list, you could apply the comma using a pseudo element and then NOT apply it on the last item:

<ul class="cat-list">
{exp:channel:categories style="linear" category_group="3"}
        {if nav_location == 'Display in primary and header navigation'}
            <li><a href="{path="{category_url_title}"}">{category_name}</a></li>
        {/if}
{/exp:channel:categories}
</ul>

<style>
    ul.cat-list li:after {
        content: ', ';
    }
    ul.cat-list li:last-child:after {
        content: '';
    }
</style>

And of course the remaining styling needed to display this inline as a comma separated list. Downside here is you'd be subject to the browser support for the :after pseudo-element though.

| improve this answer | |
  • Jean, thanks for this solutions as well. I can also possibly use jQuery to get rid of the last comma. Again, thanks for offering this path... – Marc Johns Feb 28 '13 at 21:46
1

Try backspace="1" like this:

{exp:channel:categories style="linear" category_group="3" backspace="1"}

Should remove the trailing comma from the last category.

| improve this answer | |
  • Jez, thanks for the tip, but because I have a conditional statement inside the channel:categories tag, the backspace=1 option didn't work... other suggestions? – Marc Johns Feb 28 '13 at 15:03
  • Ah, then I'm not sure there is a way to do that natively. It might be worth checking out the free GW Code Categories plugin devot-ee.com/add-ons/gwcode-categories which also has a 'backspace' option similar to EE's which might work in your conditional. – Jez Swinscoe Feb 28 '13 at 16:47
  • Thanks Jez. This is the path I've started down. Again, thanks for your help with this... – Marc Johns Feb 28 '13 at 17:06

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.