work page is showing images. Images have date field. If date is not older, than 7 days, we are showing “New image” on those images
And the goal is to show badge with number of new images on parent page (works) for each work
work page is showing images. Images have date field. If date is not older, than 7 days, we are showing “New image” on those images
And the goal is to show badge with number of new images on parent page (works) for each work
$newImageCounter = page('work')->images()->filter(function($image) {
return $image->date()->toDate() >= strtotime('-7 day', time());
})->count();
Thank you, should it be inside foreach for each work? If there, I am getting error Call to a member function images() on null
Sorry, all is working fine
adapted code.
Yes, in the loop for each work, but I didn’t read properly. It should likely be
$newImageCounter = $work->images()->filter(function($image) {
return $image->date()->toDate() >= strtotime('-7 day', time());
})->count();
Or whatever variable you are using inside the loop.
Yes, exactly
Nice, thanks a lot. Only left to find how to not display if it’s zero.
Not that complicated ![]()
<?= $newImageCounter > 0 ? $newImageCounter : '' ?>
Thanks, also checked with <?php if ($newImageCounter > 0): ?>, working well, but as I understand your code is better?
Depends, if you have some HTML, the if statement is more useful
<?php if ($newImageCounter > 0): ?>
<!--- HTML stuff here -->
<?php endif; `>
Ah, ok, I just was in a rush, as am very exiced how it’s easy with Kirby, also the client
Yes, it has span with bg style taken from page settings. Cool, thank you!
Only can’t understand why when I pass variable instead of number of days <?php $newterm = $site->newterm() ?>,
strtotime("- $newterm day", time())
getting error, that $newterm is undefined?
For child page it works well like this:
<?php if( strtotime($newwork) > strtotime("- $newterm day") ) :?>
Try
$site->newterm()->value()
Same error
Could you please post more context?
Are you definiting $newTerm outside the closure maybe? Then it will be undefined if you don’t pass it on via use().
<?php snippet('header') ?>
<?php snippet('intro') ?>
<?php $newterm = $site->newterm() ?>
<?php if ($worksPage = page('works')): ?>
<ul class="home-grid">
<?php foreach ($worksPage->children()->listed() as $work): ?>
<li>
<a href="<?= $work->url() ?>">
<figure>
<?php if ($cover = $work->cover()): ?>
<?= $cover->resize(1024, 1024) ?>
<?php endif ?>
<?php
$newImageCounter = $work->images()->filter(function($image) {
return $image->date()->toDate() > strtotime("- 20 day", time());
})->count(); ?>
<?php if ($newImageCounter > 0): ?>
<span class="new-number" style="background: <?= $work->color() ?>">
+<?= $newImageCounter ?>
</span>
<?php endif ?>
<figcaption>
<span>
<span class="example-name"><?= $work->title()->html() ?></span>
</span>
</figcaption>
</figure>
</a>
</li>
<?php endforeach ?>
</ul>
<?php endif ?>
<?php snippet('footer') ?>
As I suspected. If you want to use $newTerm within the closure…
$newImageCounter = $work->images()->filter(function($image) use($newTerm){
return $image->date()->toDate() > strtotime("- 20 $newTerm", time());
})->count(); ?>
Yes, great, that solves the question, thank you!