<p>This can also be done with a Twig macro, rather than creating a plugin:</p>

<pre><code>{% macro ordinal(number) %}
    {%- spaceless %}
    {% if ((number % 100) &gt;= 11) and ((number % 100) &lt;= 13) %}
        {{ number }}th
    {% else %}
        {% set ends = ['th','st','nd','rd','th','th','th','th','th','th'] %}
        {{ number ~ ends[number % 10] }}
    {% endif %}
    {% endspaceless -%}
{% endmacro %}

{% from _self import ordinal %}

{% for entry in craft.entries %}
    &lt;li&gt;{{ ordinal(loop.index) }}&lt;/li&gt;
{% endfor %}
</code></pre>

CC BY-SA 3.0
2015-07-23T23:14:34.470Z
Thanks Brandon. Is the an advantage/disadvantage to using a macro over a plugin?
Katrin
CC BY-SA 3.0
2015-07-17T20:03:07.733Z
2015-07-17T20:03:07.733Z
Brandon Kelly
<p>You'd want to write a plugin that added an <code>|ordinal</code> Twig <a href="http://buildwithcraft.com/docs/templating/filters" rel="noreferrer">filter</a>.</p>

<p>Then from your template, you would do: <code>loop.index|ordinal</code>.</p>

<p>The logic in the plugin would look something like:</p>

<pre><code>public function ordinal($number)
{
    $ends = array('th','st','nd','rd','th','th','th','th','th','th');

    if ((($number % 100) &gt;= 11) &amp;&amp; (($number % 100) &lt;= 13))
    {
        return $number.'th';
    }
    else
    {
        return $number.$ends[$number % 10];
    }
}
</code></pre>

CC BY-SA 3.0
2015-07-17T18:15:04.287Z
For the record, I had to Google what an ordinal suffix was.
Brad Bell
CC BY-SA 3.0
2015-07-17T18:35:00.243Z
So did I Brad...
Peter Tell
CC BY-SA 3.0
2015-07-17T19:24:01.293Z
Sorry about that. What should I have called it?
Katrin
CC BY-SA 3.0
2015-07-17T19:24:54.690Z
I'm sure that's the right word... just got to learn something new is all. :)
Brad Bell
CC BY-SA 3.0
2015-07-17T19:25:18.520Z
And thanks Brad, I'll try find some time figuring out how to add a plugin :)
Katrin
CC BY-SA 3.0
2015-07-17T19:26:36.357Z
Search for filter here: https://straightupcraft.com/craft-plugins and you'll find a bunch of plugins to get you started.
Brad Bell
CC BY-SA 3.0
2015-08-25T12:52:56.487Z
I've finally gotten around to creating [a plugin](https://github.com/katrinkerber/craft-ordinalize). Thank you for your help.
Katrin
CC BY-SA 3.0
2015-07-17T18:14:39.113Z
2015-07-17T19:25:08.920Z
2015-07-17T19:25:08.920Z
Brad Bell
<p>I'd like to add an ordinal suffix to the <code>loop.index</code> variable within a <code>for</code> loop.</p>

<p>For example:</p>

<pre><code>1st
2nd
3rd
</code></pre>

<templating><forloop>
Add ordinal suffix to loop.index
# Add ordinal suffix to loop.index

(Asked by: username_0 on 2015-07-17)

I'd like to add an ordinal suffix to the loop.index variable within a for loop.
For example:
1st
2nd
3rd

**Tags:** templating, forloop


---

## 2 answers

### Answer by username_2 on 2015-07-17. Score: 5 | Accepted answer

You'd want to write a plugin that added an |ordinal Twig filter.
Then from your template, you would do: loop.index|ordinal.
The logic in the plugin would look something like:
public function ordinal($number)
{
    $ends = array('th','st','nd','rd','th','th','th','th','th','th');

    if ((($number % 100) >= 11) && (($number % 100) <= 13))
    {
        return $number.'th';
    }
    else
    {
        return $number.$ends[$number % 10];
    }
}

> **Comments:**
> **username_2**: For the record, I had to Google what an ordinal suffix was.  
> **username_3**: So did I Brad...  
> **username_0**: Sorry about that. What should I have called it?  
> **username_2**: I'm sure that's the right word... just got to learn something new is all. :)  
> **username_0**: And thanks Brad, I'll try find some time figuring out how to add a plugin :)  
> **username_2**: Search for filter here: https://straightupcraft.com/craft-plugins and you'll find a bunch of plugins to get you started.  
> **username_0**: I've finally gotten around to creating [a plugin](https://github.com/katrinkerber/craft-ordinalize). Thank you for your help.  

---

### Answer by username_1 on 2015-07-17. Score: 7

This can also be done with a Twig macro, rather than creating a plugin:
{% macro ordinal(number) %}
    {%- spaceless %}
    {% if ((number % 100) >= 11) and ((number % 100) <= 13) %}
        {{ number }}th
    {% else %}
        {% set ends = ['th','st','nd','rd','th','th','th','th','th','th'] %}
        {{ number ~ ends[number % 10] }}
    {% endif %}
    {% endspaceless -%}
{% endmacro %}

{% from _self import ordinal %}

{% for entry in craft.entries %}
    {{ ordinal(loop.index) }}
{% endfor %}

> **Comments:**
> **username_0**: Thanks Brandon. Is the an advantage/disadvantage to using a macro over a plugin?  

---

