I am attempting to build galleries of photos based on {photo_date}, a custom field containing the date on which each photo was taken. My list of photos looks something like this:

Photo#   {photo_date}
1        11/1/14
2        11/1/14
3        11/1/14
4        11/16/14
5        11/16/14
6        11/22/14

My goal is to have a loop that allows only one photo to appear from each date "group." This photo will serve as the photo group's cover image. So, after running the script, I will end up with this:

Group 1: Photo#1 (11/1/14)
Group 2: Photo#4 (11/16/14)
Group 3: Photo#6 (11/22/14)

I am attempting to use a php variable inside of an EE loop to check to see if it matches:

           <?php $clip = "0"; ?>
           {exp:channel:entries channel="event_photos" dynamic="no" sort="asc" orderby="photo_date" search:photo_date="not IS_EMPTY"}
            <div class="clear floatnone">
                {if "{photo_event}{photo_event:url_title}{/photo_event}" == "<?php echo $currentSegment; ?>"} <!-- filter only this event's photos -->
                    Before add: <?php echo $clip; ?><br />
                    {if "{photo_date format='%m/%d/%y'}" != "<?php echo $clip; ?>"}
                        {title}<br />
                        <a href="{photo}" data-lightbox="{photo_date format="%m/%d/%y"}" data-title="{photo_caption}"><img width="150px" src="{photo}" /></a>
                    <?php $clip = "{photo_date format='%m/%d/%y'}"; ?>
                    After add: <?php echo $clip; ?><br />

I think this should work, but it doesn't. It seems that the PHP variable ($clip) is getting assigned at the end of the {if} loop, but gets erased at the beginning once the loops starts over. This prevents me from checking to see if the {photo_date} in question has been used or not in the previous iteration. I'm wondering how to get the variable to retain the intended value?

I appreciate any thoughts or maybe a more elegant solution!

3 Answers 3


Have you considered just using Channel Images?

  • I don't have access to the Channel Images module right now. Do you have another suggestion? :/ Commented Dec 2, 2014 at 16:38

OK - so after reading this a few more times I think I have a clearer sense of what you may be doing.

If you're relating the event photos in one channel to events in another (I'm guessing this based on your [{photo_event}{photo_event:url_title}{/photo_event}] code, I think I'd do it the other way.

Start by running a query to select the date in question - use select distinct (or the :activerecord plugin ) Then relate that date to your photo and limit the relationship field to one result (try limit="1") in the tag. That should give you your dates with an image for each to correspond - with no php in your templates.

Also - not sure what/why that $currentSegment is in your code, but maybe check out Segment variables for what you need there?

If this is off base, then I still don't get what you're trying to do. :)


You could do this with the Stash add-on. There's an example in the docs called Sort a list of entries by year and then by month. You could use a simpler version of that example for your needs.

I created an example that doesn't include all of your HTML markup and custom fields (photo, photo_caption, etc.). But this should get you started.

Step by step it works like this.

First, set a list of all your entries:

{exp:stash:set_list name="entries" parse_tags="yes"}
    {exp:channel:entries channel="event_photos" dynamic="no" sort="asc" orderby="photo_date" search:photo_date="not IS_EMPTY"}
        {stash:date_sort}{photo_date format="%m/%d/%y"}{/stash:date_sort}

Make a copy of the same list containing only the date_sort dates (mm/dd/yyyy) so we can find all unique dates:

{exp:stash:set_list name="dates" parse_tags="yes"}
  {exp:stash:get_list name="entries"}

Then output the list of unique dates and a list of entries that match that unique date. The date_machine is used to order them within each unique date.

{exp:stash:get_list name="dates" unique="yes" orderby="date_sort" sort="desc"}



      {if entry:count==1}

      <li>{entry:title} ({entry:date_sort})</li>

      {if entry:count==entry:total_results}


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.