0

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>
                    {/if}
                    <?php $clip = "{photo_date format='%m/%d/%y'}"; ?>
                    After add: <?php echo $clip; ?><br />
                {/if}
            </div>
            {/exp:channel:entries}

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!

0

Have you considered just using Channel Images?

| improve this answer | |
  • I don't have access to the Channel Images module right now. Do you have another suggestion? :/ – Paredes Creative Dec 2 '14 at 16:38
0

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. :)

| improve this answer | |
0

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:title}{title}{/stash:title}
        {stash:date_sort}{photo_date format="%m/%d/%y"}{/stash:date_sort}
        {stash:date_machine}{entry_date}{/stash:date_machine}
    {/exp:channel:entries}
{/exp:stash:set_list}

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"}
      {stash:date_sort}{date_sort}{/stash:date_sort}
  {/exp:stash:get_list}
{/exp:stash:set_list}

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"}

  <h1>{date_sort}</h1>

    {exp:stash:get_list:entries 
        name="entries" 
        match="#^{date_sort}$#" 
        against="date_sort" 
        orderby="date_machine" 
        sort="asc" 
        prefix="entry"
    }

      {if entry:count==1}
      <ul>
      {/if}

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

      {if entry:count==entry:total_results}
      </ul>
      {/if}
    {/exp:stash:get_list:entries}


{/exp:stash:get_list} 
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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