1

I've been looking around at numerous posts but can't seem to find a solution that works without constant work arounds. I'm looking to show posts split by days they were posted similar to www.producthunt.com

Today, Yesterday -> Sunday. Then displaying the date posted e.g. 31st March from then on.

Different to most questions as this is showing live posts set out by different worded days instead of numerical. I can't seem to find a way of doing this without repeating the same code block for 7 days then a generic by date loop?

Is this possible in Craft?

3
2

This seems like a good use case for Craft's group filter. I'd recommend reading this page to get a better understanding of how it's used.

Not quite sure what your code looks like but I'll pull this from the above mentioned support article for explanation purposes:

{% set allEntries = craft.entries.section('blog').limit(null) %}

{% for month, entries in allEntries | group("postDate|date('F')") %}
    <h3>{{ month }}</h3>
    <ul>
        {% for entry in entries %}
            <li>{{ entry.getLink() }}</li>
        {% endfor %}
    </ul>
{% endfor %}

So obviously you'd need to adapt this code to your setup, for example, switch out the month grouping for a per-day grouping. Then what I would do is wrap it in a conditional that only works if the postDate is more than three days older than today's date. Then you could use a similar query as the above to display the first three sections individually, (i.e. one for "Today", one for "Yesterday", and one for the day of the week of the third day).

Might not be the best way (curious to see any other answers that might show up), but just the first thought that came to mind from a quick perusal of your question.

2
  • Hi Jalen, thanks very much for the help! It's definitely the way I was thinking but having numerous code snippets for multiple days seems a bit clunky? I may have a dig around and see what I can do
    – JMKelley
    Apr 3 '18 at 8:48
  • Yeah I guess it sounds a bit clunky. If you do go this route, I would recommend maybe eager-loading your entries to improve performance and reduce the number of database queries. Apr 3 '18 at 12:27

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.