Output Results by the first letter of title

  • I've been looking through the Docs and Forums for a while now but can't figure out a solution for this.

    I have this page of a website on a CMS that is closing down (Adobe Business Catalyst) http://tuffyworld.com.au/songlist I'm trying to replicate this page in perch, but I can't figure out the best way to output the items.

    HTML: Songlist item template
    1. <div class="songTitle">
    2.     <perch:content id="songName" type="smarttext" label="Song Title" title>
    3.     <perch:if exists="songArtist">
    4.     <div class="artistYes"><em>By</em> (<perch:content id="songArtist" type="smarttext" label="Artist">)</div>
    5. </perch:if>
    6. </div>

    PHP: Perch Content Custom
    1. <?php perch_content_custom('Song List', array(
    2.     'filter' => substr($title, 0,1),
    3.     'sort' => 'songName',
    4.     'match' => 'contains',
    5.     'value' => '2',
    6. )); ?>

    I can't even get this to output only the first item with a '2' in the name all items are showing.

    Sorry if this is a stupid question, I'm new to perch, LOVING IT, just my PHP knowledge is fairly basic (I've got 2 live website working great on perch with more to come)

    Can you please give me some direction with this issue.

    Thanks in advance

  • drewm

    Approved the thread.
  • Hi

    I believe your filter value needs to be equal to a Perch field ID. You could try using `'match' => 'regex',` and use a PCRE expression that mirrors your requirement as the value e.g. `^2` – NB I am not good at REGEX so this might not be right for you!

  • Thanks ellimondo

    I'll look into it, my PHP knowledge is pretty limited I might just make a select field for my customer to select a letter the song starts with and then just have a heap of filters to pull each letters result. I'd like to make it more automatic, but what you wrote above has gone straight over my head

  • You can group items without filtering from within the template.

    Output the list in the order you need:

    1. perch_content_custom('Song List', array(
    2. 'sort' => 'songName',
    3. 'sort-order' => 'ASC',
    4. ));

    You can use conditional tags plus the format attribute inside the template to achieve what you want.

    The following outputs the first character of a field:

    1. <perch:content id="songName" format="C:1">

    The following checks the previous item against the current item:

    1. <perch:if different="songName" format="C:1" format-both>
    2. The first character of the previous item's songName is different than the current item's
    3. </perch:if>

    If you want the rendered HTML to be:

    You can use something like this: