Show and hide content with different dates

Hello everyone,

I’ve got a small information page where specific information should be shown at a specific date and be hidden at another speicifc date.

Following fields have been defined:

datepublish - the first day the information should be shown.
dateexpired - the last day the information should be shown.

I’ve tried to work with following solution which is not working:

<?php $items = $site->index()->filterBy('template', 'ticker')->filterBy('datepublish', '<=', time())->filterBy('dateexpired', '>=', time())->sortBy('datepublish', 'desc')->paginate(10);
if($items->count()):
  foreach( $items as $item): ?>
...
<?php endforeach ?>

The datepublish and dateexpired are stored in YYYY-MM-DD format.

Hi,

You might have a look at


Edit: Nevermind the above; they are Kirby 3 plugins and I just saw it’s for Kirby 2.
I’m not sure if we had plugins for that in K2…

1 Like

There is no need to unpublish it from panel, just to show/hide it for visitors. Shouldn’t filterBy work for that?

In the filterBy() I think you’re comparing strings to int’s.

I think you should be able to use the filter()-method with the callback and convert your timestamps as Datetime-objects. Like this to filter only the pages that can be published:

$pages->filter(function ($page) {                        
    $publishdate = new Datetime($page->datepublish());
    return $publishdate->getTimestamp() <= time();
});

:warning: Be aware that this does not work with caching enabled!

1 Like

Yes, but you need an and here

$site->index()->filter(function ($page) {                        
    $publishdate     = $page->date(null, 'datepublish');
    $unpublishdate =   $page->date(null, 'dateexpire');
    return $publishdate >= time() && $unpublishdate <= time();
});
1 Like

@bvdputte @texnixe - good morning and thanks for replies!

Could you help me about right integration? Specificaly how to integrate this:

<?php $site->index()->filter(function ($page) {                        
$publishdate     = $page->date(null, 'datepublish');
$unpublishdate =   $page->date(null, 'dateexpire');
return $publishdate >= time() && $unpublishdate <= time();
}); ?>

into this

<?php $items = $site->index()->filterBy('template', 'ticker')->filterBy('datepublish', '<=', time())->filterBy('dateexpire', '>=', time())->sortBy('datepublish', 'desc')->paginate(10); if($items->count()): foreach( $items as $item): ?>
<?= $item->title() ?> 
<?= $item->text()->kt() ?>
<?php endforeach ?>
<?php endif ?>

:innocent:

Untested, but this should work:

<?php

$items = $site->index()
              ->filterBy('template', 'ticker')
              ->filter(function ($page) {                        
                $publishdate     = $page->date(null, 'datepublish');
                $unpublishdate =   $page->date(null, 'dateexpire');
                return $publishdate >= time() && $unpublishdate <= time();
              })
              ->sortBy('datepublish', 'desc')
              ->paginate(10);

if($items->count()):
    foreach( $items as $item): ?>
        <?= $item->title() ?> 
        <?= $item->text()->kt() ?>
    <?php endforeach ?>
<?php endif ?>

If you intend on needing this elsewhere too, it might be handy to turn this into a custom collection. Kinda like this:

// `/site/collections/ticker.php`
return function ($site) {
    return $site->index()
                ->filterBy('template', 'ticker')
                ->filter(function ($page) {                        
                    $publishdate     = $page->date(null, 'datepublish');
                    $unpublishdate =   $page->date(null, 'dateexpire');
                    return $publishdate >= time() && $unpublishdate <= time();
                })
                ->sortBy('datepublish', 'desc')
                ->paginate(10);
};

Then you can use it everywhere like this:

$items = $kirby->collection("ticker");
if($items->count()):
    foreach( $items as $item): ?>
        <?= $item->title() ?> 
        <?= $item->text()->kt() ?>
    <?php endforeach ?>
<?php endif ?>

Edit: forget the custom collection-suggestion I made, that’s for Kirby 3 as well…

You see, there’s lot’s of niceties in Kirby 3. You might consider an upgrade :stuck_out_tongue_winking_eye:

1 Like

I’m almost there - I’ve implemented solution as adviced. I didn’t get any error but nothing is shown, this is how it’s stored in .txt file

Title: Petra Mustermann startet am 1. Juni
----
Datepublish: 2019-04-24
----
Dateexpire: 2019-05-30

I know, Kirby 3 is fantastic. I’m still in learning proccess to use it and the project I’ve developed in Kirby 2 was allready developed for several months and client want to keep K2 panel look so I respect that. This is practicaly my last K2 project :partying_face:

One more thing:

Can these pages with the ticker template really be anywhere? Going through the complete index of pages should really be avoided–at least if you have many pages–wherever possible.

This is the structure, the template where I intend to use snippets is home.php

image

Ok, there is an error in the logic

And if the pages are all children of home, you don’t have to go through the site index

$items = page('home')
              ->index()
              ->filterBy('template', 'ticker')
              ->filter(function ($page) {                        
                $publishdate     = $page->date(null, 'datepublish');
                $unpublishdate =   $page->date(null, 'dateexpire');
                return $publishdate >= time() && $unpublishdate <= time();
              })
              ->sortBy('datepublish', 'desc')
              ->paginate(10);

Hope that works now.

1 Like

Unfortunately it’s not working. Nothing is displayed - I’m trying to troubleshoot problem.

I’ve found error

This was suggested:

$publishdate >= time() && $unpublishdate <= time();

correct way is:

$publishdate <= time() && $unpublishdate >= time();

And another day I’ve learned something new :beers:

That’s what I meant with the error in the logic and then was too stupid to post it correctly :see_no_evil:

1 Like

It happens - just usual friday. It’s time for weekend :dizzy:

1 Like