I created a blog that doesn’t display article dates, but I want the articles/posts to display with the most recent posts first. I’ve tried all sorts of ->sortBy() variants, including pubdate and publish_date with no success.
Probably not saying this right, but is there any way for me to call the creation/publish date without specifying a date at the time of creating the post? I feel like there is something very simple I’m missing.
Not sure this helps at all, but here is my base loop without any sort (I don’t specify visible because my blog posts are invisible…I don’t want them displaying in the navigation dropdown):
<?php $articles = $page->children()->flip()->paginate(10) ?>
<?php foreach($articles as $article): ?>
Thank you. I originally designed it without the date field because I thought the posts would be “evergreen” and not produced very often, hence I didn’t want a date drawing attention to the fact the posts were old. Of course, I should have built it in anyway and just not displayed the date, esp considering now I want to both call the date and display it.
Is there a way to retro-fit the date into the existing posts? I tried various methods of adding the date with little success, even on newly created posts. I was able to add a date field via the post’s blueprint (using both date and datetime), thinking I could at least go back and manually assign dates to get the order to display correctly. But I was unable to get my sort function to work. I was also unable to get the date to display on either the main page or the post page. I found multiple ways other people accomplish this, but none of them seemed to work for me.
Here is what is currently on my main page. This displays all posts in what appears to be reverse alphabetical order according to the post’s title.
pages:
template: newsarticle
num: date
field: date
sort: flip
The setting you have in your blueprint is not a field, it only defines how subpages are numbered when you make them visible and how they are sorted. You have to add a date field to the newsarticle blueprint.
fields:
# ... some more fields
date:
label: Date
type: date
I apologize, I should have also included my newsarticle blueprint, which already includes the date field exactly as you have it. I am able to add dates in the panel, I am just unable to call the date in order to sort OR to display despite various efforts based on research here and through others’ blog templates.
Yes, the front end displays correctly now! I tried the ->sortBy('date', 'desc') previously, but maybe my inclusion of ->flip() , as well, may have been messing things up. Same thing with my main page/news blueprint file. Using sort: date desc instead of sort: flip fixed my display issue in the panel view.
As for displaying the date, that now works, as well. I just need to research/learn learn how to display the format differently. Thank you!
Simply needed to look up what php date format to use to output a textual representation of the month and a single digit version of the date. I ended up with this version, and everything looks great.