Skip to main content
removed unnecessary code
Source Link
afc
  • 67
  • 7

I've got a for loop that is returning the first letter of the entry title. I have a number of entries so I almost get a complete alphabet except that there are too many of many letters. I only want one 'a', one 'b', one 'c' and so forth. And no letter if there is no entry to correspond to it.

{% for alpha in craft.entries.section('advertisers').order('title asc') %}
    <a href="{{ siteUrl }}{{ alpha.title|first }}">{{ alpha.title|first }}</a>
{% endfor %}
 
{% set letter = slug is defined and slug|length == 1 and slug matches '/^[a-z]/i' ? slug %}

{% if not letter %}
  {% exit 404 %}
{% endif %}

{% set entries = craft.entries.section('advertisers').title(letter~'*').limit(null).order('title asc') %}

This code returns: A A A B B B B B C C E E E E G G H H K M M M M P P R R S S S T T U U U W W but I want it to return A B C E G H K M P R S T U W

Are there any solutions to this?

I've got a for loop that is returning the first letter of the entry title. I have a number of entries so I almost get a complete alphabet except that there are too many of many letters. I only want one 'a', one 'b', one 'c' and so forth. And no letter if there is no entry to correspond to it.

{% for alpha in craft.entries.section('advertisers').order('title asc') %}
    <a href="{{ siteUrl }}{{ alpha.title|first }}">{{ alpha.title|first }}</a>
{% endfor %}
 
{% set letter = slug is defined and slug|length == 1 and slug matches '/^[a-z]/i' ? slug %}

{% if not letter %}
  {% exit 404 %}
{% endif %}

{% set entries = craft.entries.section('advertisers').title(letter~'*').limit(null).order('title asc') %}

This code returns: A A A B B B B B C C E E E E G G H H K M M M M P P R R S S S T T U U U W W but I want it to return A B C E G H K M P R S T U W

Are there any solutions to this?

I've got a for loop that is returning the first letter of the entry title. I have a number of entries so I almost get a complete alphabet except that there are too many of many letters. I only want one 'a', one 'b', one 'c' and so forth. And no letter if there is no entry to correspond to it.

{% for alpha in craft.entries.section('advertisers').order('title asc') %}
    <a href="{{ siteUrl }}{{ alpha.title|first }}">{{ alpha.title|first }}</a>
{% endfor %}

This code returns: A A A B B B B B C C E E E E G G H H K M M M M P P R R S S S T T U U U W W but I want it to return A B C E G H K M P R S T U W

Are there any solutions to this?

added 4 characters in body
Source Link
Brad Bell
  • 67.6k
  • 6
  • 75
  • 145

I've got a for loop that is returning the first letter of the entry title. I have a number of entries so I almost get a complete alphabet except that there are too many of many letters. I only want one 'a', one 'b', one 'c' and so forth. And no letter if there is no entry to correspond to it.

{% for alpha in craft.entries.section('advertisers').order('title asc') %}
    <a href="{{ siteUrl }}{{ alpha.title|first }}">{{ alpha.title|first }}</a>
{% endfor %}

{% set letter = slug is defined and slug|length == 1 and slug matches '/^[a-z]/i' ? slug %}

{% if not letter %}
  {% exit 404 %}
{% endif %}

{% set entries = craft.entries.section('advertisers').title(letter~'*').limit(null).order('title asc') %}

This code returns: A A A B B B B B C C E E E E G G H H K M M M M P P R R S S S T T U U U W W but I want it to return A B C E G H K M P R S T U W

Are there any solutions to this?

I've got a for loop that is returning the first letter of the entry title. I have a number of entries so I almost get a complete alphabet except that there are too many of many letters. I only want one 'a', one 'b', one 'c' and so forth. And no letter if there is no entry to correspond to it.

{% for alpha in craft.entries.section('advertisers').order('title asc') %}
<a href="{{ siteUrl }}{{ alpha.title|first }}">{{ alpha.title|first }}</a>
{% endfor %}

{% set letter = slug is defined and slug|length == 1 and slug matches '/^[a-z]/i' ? slug %}

{% if not letter %}
  {% exit 404 %}
{% endif %}

{% set entries = craft.entries.section('advertisers').title(letter~'*').limit(null).order('title asc') %}

This code returns: A A A B B B B B C C E E E E G G H H K M M M M P P R R S S S T T U U U W W but I want it to return A B C E G H K M P R S T U W

Are there any solutions to this?

I've got a for loop that is returning the first letter of the entry title. I have a number of entries so I almost get a complete alphabet except that there are too many of many letters. I only want one 'a', one 'b', one 'c' and so forth. And no letter if there is no entry to correspond to it.

{% for alpha in craft.entries.section('advertisers').order('title asc') %}
    <a href="{{ siteUrl }}{{ alpha.title|first }}">{{ alpha.title|first }}</a>
{% endfor %}

{% set letter = slug is defined and slug|length == 1 and slug matches '/^[a-z]/i' ? slug %}

{% if not letter %}
  {% exit 404 %}
{% endif %}

{% set entries = craft.entries.section('advertisers').title(letter~'*').limit(null).order('title asc') %}

This code returns: A A A B B B B B C C E E E E G G H H K M M M M P P R R S S S T T U U U W W but I want it to return A B C E G H K M P R S T U W

Are there any solutions to this?

Source Link
afc
  • 67
  • 7

How do I omit similar content in a for loop based on the first letter in title?

I've got a for loop that is returning the first letter of the entry title. I have a number of entries so I almost get a complete alphabet except that there are too many of many letters. I only want one 'a', one 'b', one 'c' and so forth. And no letter if there is no entry to correspond to it.

{% for alpha in craft.entries.section('advertisers').order('title asc') %}
<a href="{{ siteUrl }}{{ alpha.title|first }}">{{ alpha.title|first }}</a>
{% endfor %}

{% set letter = slug is defined and slug|length == 1 and slug matches '/^[a-z]/i' ? slug %}

{% if not letter %}
  {% exit 404 %}
{% endif %}

{% set entries = craft.entries.section('advertisers').title(letter~'*').limit(null).order('title asc') %}

This code returns: A A A B B B B B C C E E E E G G H H K M M M M P P R R S S S T T U U U W W but I want it to return A B C E G H K M P R S T U W

Are there any solutions to this?