Hi, I’m a bit confused about ordering new blog posts from different subpages so that the last post is the first post shown on the homepage. My structure is like this:
categories
I’m fetching the posts like this on the homepage:
<?php foreach($pages->children()->sortBy('modified', 'desc')->visible() as $category): ?>
<?php foreach($category->children()->sortBy('modified', 'desc')->visible() as $post): ?>
<?php foreach($post->thumbnail() as $image): ?>
<?php if($img = $post->image($image)): ?>
<div class="col b50 center reveal">
<div class="box spacing">
<a href="<?php echo $post->url(); ?>">
<?php snippet('responsive_image', array('image' => $img, 'sizes' => [512, 1024, 1280, 1440])) ?>
</a>
<p class="caption mono-xs">
<?php echo $page->modified('d/m/Y H:i') ?>
</p>
</div>
</div>
<?php endif ?>
<?php endforeach ?>
<?php endforeach ?>
<?php endforeach ?>
I can sort the posts, but only within its category, but I want that the last post to be shown as the first item.
I tried everything from flip to date sorting, but nothing works.
Thank for any help
Assuming your category pages are on the first level (i.e. a direct subfolder of content and they share the same intended template to filter by - e.g. category.txt), you can pull them into a single collection like this:
<?php
$posts = $pages->children()->filterBy('intendedTemplate', 'category')->children()->sortBy('modified', 'desc');
foreach($posts as $post): ?>
<?php foreach($post->thumbnail() as $image): ?>
<?php if($img = $post->image($image)): ?>
<div class="col b50 center reveal">
<div class="box spacing">
<a href="<?php echo $post->url(); ?>">
<?php snippet('responsive_image', array('image' => $img, 'sizes' => [512, 1024, 1280, 1440])) ?>
</a>
<p class="caption mono-xs">
<?php echo $page->modified('d/m/Y H:i') ?>
</p>
</div>
</div>
<?php endif ?>
<?php endforeach ?>
<?php endforeach ?>
Edit: Are you sure you want to call $page->modified()
and not $post->modified()
?
1 Like
Uh, nice… Thanks for helping. Works exactly how I want it
Edit: yes… I also just noticed it and changed it to $post->modified()
Whenever I make a change in another post than this post will be the first on the homepage, is there a way of ignoring this? That only the newest post will be the first. I guess it has something to do with: sortBy(‘modified’, ‘desc’); ?
Well, yes, you are sorting by modified. Your best bet is using a date or - depending on the frequency of your posts - even a datetime field.
I just tried it but it it’s not working:
Also, the time in the panel shows me the actual time - 1 hour.
datetime:
label: Date
datetime:
type: datetime
date:
format: DD/MM/YYYY
default: now
time:
interval: 1
default: now
<?php $posts = $pages->children()->filterBy('intendedTemplate', 'category')->children()->sortBy('date', 'desc', 'time', 'desc'); foreach($posts as $post): ?>
<?php foreach($post->thumbnail() as $image): ?>
<?php if($img = $post->image($image)): ?>
<div class="col b50 center reveal">
<div class="box spacing">
<a href="<?php echo $post->url(); ?>">
<div class="thumb">
<?php snippet('responsive_image', array('image' => $img, 'sizes' => [512, 1024, 1280, 1440])) ?>
</div>
</a>
<p class="caption mono-xs">
<?php echo $post->datetime('d/m/Y') ?>
</p>
</div>
</div>
<?php endif ?>
<?php endforeach ?>
The field in the blueprint should look similar to this:
datetime:
type: datetime
date:
format: DD-MM-YYYY
time:
format: 12
interval: 15
default:
date: now
time: now
Your machine is probably as stupid as all the others and needs a little help, so give it a timezone to work with in your config:
Example for Berlin:
c::set('timezone', 'Europe/Berlin');
I see… the time is now correct but the sorting not. Its still sorting only within its category. Any ideas?
$posts = $pages->children()->filterBy('intendedTemplate', 'category')->children()->sortBy('datetime', 'desc');
foreach($posts as $post):
echo $post->date('Y-m-d H:i', 'datetime');
endforeach;
DennisA
January 17, 2018, 7:11pm
10
ohh… stupid me… ‘datetime’ not ‘date’… thank you so much texnixe