Answers.Body: <p>You can use the <a href="https://docs.craftcms.com/v2/templating/filters.html#filter" rel="nofollow noreferrer">group</a> filter and create a 2d array with the letter as the first index and the entries as second index. </p>
  
  <pre class="lang-twig prettyprint-override"><code>{% set allEntriesByFirstCharacter = allEntries|group('companyName|slice(0, 1)|lower|replace("/[^a-z]/", "0-9")' ) %}
  </code></pre>
  
  <p>The string passed in the filter will get rendered as an object template an equal results will be grouped by the same key. </p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-30T16:57:12.677Z
Answers.Text: Thank you! I'm getting empty outputs, but the right number of instances now. Post updated.
Answers.UserDisplayName: sarah3585
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-30T16:59:38.277Z
Answers.Text: Where do you assign the `entry` variable? You use it but I'm not sure what it should be. Please take a look at the link
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-30T15:36:02.920Z
Answers.LastActivityDate: 2018-10-30T15:36:02.920Z
Answers.UserDisplayName: Robin Schambach
Body: <p>I have a A-Z menu to filter entries by their first letter.</p>
  
  <pre><code>   &lt;ul class="directory-letters"&gt;
      &lt;li class="all current" data-filter="all"&gt;ALL&lt;/li&gt;
      {% for entry in craft.entries.section('membershipDirectory').order('title asc') %}
        &lt;li data-filter="letter-{{ entry.companyName|slice(0, 1)|lower|replace("/[^a-z]/", "0-9") }}"&gt;{{ entry.companyName|slice(0, 1) }}&lt;/li&gt;
      {% endfor %}
     &lt;/ul&gt;
  </code></pre>
  
  <p>Issue I have is if there are 4 entries beginning with 'C' the output will be:</p>
  
  <p>A B C C C C D E F...</p>
  
  <p>Can I limit each letter once so I'll get 1 'C' is there are 1+ entries starting with 'C'?</p>
  
  <hr>
  
  <p>UPDATE:</p>
  
  <p>I'm now getting the right number of instances created but no text inside my <li>. Likely a rookie error somewhere:</p>
  
  <pre><code>{% set allEntries = 
  craft.entries.section('membershipDirectory').order('title asc') %}
  {% set allEntriesByFirstCharacter = 
   allEntries|group('companyName|slice(0, 1)|lower|replace("/[^a-z]/", 
   "0-9")' ) %}
  
   &lt;ul class="directory-letters"&gt;
      &lt;li class="all current" data-filter="all"&gt;ALL&lt;/li&gt;
      {% for allEntries in allEntriesByFirstCharacter %}
         &lt;li data-filter="letter-{{ entry.companyName|slice(0, 1)|lower|replace("/[^a-z]/", "0-9") }}"&gt;{{ entry.companyName|slice(0, 1)|lower|replace("/[^a-z]/", "0-9") }}&lt;/li&gt;
    {% endfor %}
  &lt;/ul&gt;
  </code></pre>
  
Tags: <entries><filter>
Title: A-Z filter menu - limiting output to one when there are multiples
ThreadText: # A-Z filter menu - limiting output to one when there are multiples
  
  (Asked by: username_0 on 2018-10-30)
  
  I have a A-Z menu to filter entries by their first letter.
  
  ```
     <ul class="directory-letters">
      <li class="all current" data-filter="all">ALL</li>
      {% for entry in craft.entries.section('membershipDirectory').order('title asc') %}
        <li data-filter="letter-{{ entry.companyName|slice(0, 1)|lower|replace("/[^a-z]/", "0-9") }}">{{ entry.companyName|slice(0, 1) }}</li>
      {% endfor %}
     </ul>
  
  ```
  
  Issue I have is if there are 4 entries beginning with 'C' the output will be:
  
  A B C C C C D E F...
  
  Can I limit each letter once so I'll get 1 'C' is there are 1+ entries starting with 'C'?
  
  ---
  
  UPDATE:
  
  I'm now getting the right number of instances created but no text inside my
  
  - . Likely a rookie error somewhere:
  
    ```
    {% set allEntries = 
    craft.entries.section('membershipDirectory').order('title asc') %}
    {% set allEntriesByFirstCharacter = 
     allEntries|group('companyName|slice(0, 1)|lower|replace("/[^a-z]/", 
     "0-9")' ) %}
  
     <ul class="directory-letters">
        <li class="all current" data-filter="all">ALL</li>
        {% for allEntries in allEntriesByFirstCharacter %}
           <li data-filter="letter-{{ entry.companyName|slice(0, 1)|lower|replace("/[^a-z]/", "0-9") }}">{{ entry.companyName|slice(0, 1)|lower|replace("/[^a-z]/", "0-9") }}</li>
      {% endfor %}
    </ul>
  
    ```
  
  **Tags:** entries, filter
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-10-30. Score: 1
  
  You can use the [group](https://docs.craftcms.com/v2/templating/filters.html#filter) filter and create a 2d array with the letter as the first index and the entries as second index.
  
  ```twig
  {% set allEntriesByFirstCharacter = allEntries|group('companyName|slice(0, 1)|lower|replace("/[^a-z]/", "0-9")' ) %}
  
  ```
  
  The string passed in the filter will get rendered as an object template an equal results will be grouped by the same key.
  
  > **Comments:**
  > **username_0**: Thank you! I'm getting empty outputs, but the right number of instances now. Post updated.  
  > **username_1**: Where do you assign the `entry` variable? You use it but I'm not sure what it should be. Please take a look at the link  
  
  ---
  
  