Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

How would I find the grav page folder that has the most recent file?

Started by Muut Archive 10 years ago · 3 replies · 362 views
10 years ago

I want to have a most recent photo feature on my homepage.

{% for p in page.find('/photography').children.order('date', 'desc').slice(0, 1) %}

The above code finds the most recent page in a blog but what I want to do is find the folder that has the most recent photo in it. My folder structure is like this:

YAML
photo gallery
- album 1
  -picture1.jpg
  -picture2.jpg
- album 2
  -picture1.jpg
  -picture2.jpg

So when I drop a new photo in one of the folders, I want the homepage to know which folder has the newest file.

10 years ago

this is a little tricky because the latest media modification date is not trickled up into page, so you have to go digging to get it. Read through it and it should make sense what it's doing...

This is in Twig-friendly sytnax, but could easily be converted into pure PHP (as in a plugin).

TWIG
{% set newest_image = '' %}
{% set newest_image_timestamp = 0 %}

{% for album in page.children %}
  {% for image in album.media.images %}
    {% if image.get('modified') > newest_image_timestamp %}
      {% set newest_image_timestamp = image.get('modified') %}
      {% set newest_image = image %}
    {% endif %}
  {% endfor %}
{% endfor %}

Newest Image: {{ newest_image }}
10 years ago

Thanks again @rhukster. I will work with this and try to pull the grav page name too so i can make a link to the album. I will post my code when done but I am away right now. This would be a good plugin to have, I will see if there is anything similar already.

10 years ago

Here is the code that links the image to the gallery.

TWIG
{% set newest_image = '' %}
{% set newest_image_timestamp = 0 %}
{% set newest_image_folder = '' %}

{% for album in page.find('/photography').children %}
  {% for image in album.media.images %}
    {% if image.get('modified') > newest_image_timestamp %}
      {% set newest_image_timestamp = image.get('modified') %}
      {% set newest_image_folder = album.url %}
      {% set newest_image = image %}
    {% endif %}
  {% endfor %}
{% endfor %}

Newest Image: <a href="{{ newest_image_folder }}">{{ newest_image.cropResize(300, 300) }}</a>

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1352 9 years ago
Archive · by Muut Archive, 9 years ago
2 935 9 years ago
Archive · by Muut Archive, 9 years ago
2 4063 9 years ago
Archive · by Muut Archive, 9 years ago
1 2949 9 years ago
Archive · by Muut Archive, 9 years ago
3 1119 9 years ago