ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Comma-Delimited Category String/List That Uses "And" Before Last Category

November 11, 2011 5:08pm

Subscribe [4]
  • #1 / Nov 11, 2011 5:08pm

    FrankJohnson

    150 posts

    Hi. I’m trying to create a string of category names (from the categories a particular entry is placed in) that is comma-delimited, but also uses an “and” before the last category. Something like this:

    category1, category2, category3, and category4

    I can get the commas fairly easily using this code:

    {categories backspace="2"}{category_name}, {/categories}

    but adding the “and” before the last category name has me stumped.

    Any ideas?

    Thanks in advance,

    Frank

  • #2 / Nov 11, 2011 5:51pm

    Rob Allen

    3114 posts

    Not tested but have you tried:

    {categories backspace="2"}
    {if count == total_results} and {/if}{category_name}, {/categories}
  • #3 / Jan 01, 2012 4:45pm

    benwellby

    17 posts

    Hello,

    I’m using a “checkboxes” custom field to output multiple options (Disciplines). I’ve tried bluedreamers example, it almost works, but not quite. This is the final result:

    Discipline: and IA, Design, Frontend development, CMS (Expression Engine)


    As you can see the “and” is in the wrong place. Any ideas?

    Thanks

  • #4 / Jan 02, 2012 3:01pm

    I don’t believe count or total_results are available inside of the categories tag pair.

    You could do it with CSS, with something like this…

    EE Template:

    Discipline {categories backspace="2"}<span>{category_name}</span>, {/categories}

    CSS:

    .discipline span:last-child:before{ content: "and "; }
.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases