Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Themes & Styling

Page collection : Random but not self

admin theme

Solved by Valentin Bossens View solution

Started by Valentin Bossens 4 years ago · 8 replies · 540 views
4 years ago

Hey all,

I'm currently developing an agency portfolio website where, on the projects pages, I'm displaying a random picked project at the end of the page. The idea is to have the user to be redirected to a random project in order to discover more about what the agency is doing.

The problem is that, sometimes, the displayed project is the same that we're currently looking at ...

Is there a way to "exclude self" so the random picked project is always another one than the current one?

Here's the code I'm using for now:

TWIG
{% set random = random(page.find('/projects').children().published()) %}

Thanks in advance

4 years ago

Try page.siblings() instead of page.find('/projects').children()

4 years ago

Thanks for you fast answer.

Sadly, it looks like it can still displays the current page ...

4 years ago

Then maybe a bug, because it should not. According to docs

'@self.siblings' - A collection of all other pages on this level

4 years ago

Yes, from what I understand from the documentation, it shouldn't displays the current page ...

@self.siblings - All the sibling pages
This collection will collect all the published Pages at the same level of the current page, excluding the current page:

Maybe it's due to the random() function ...

4 years ago

random() cannot get a page and add it to collection. Could you debug siblings and see if current page is really there in the list? Is it possible you have a duplicate page which seems like the same page, but actually isn't?

4 years ago

@Valord, It is often not appreciated if a post is cross-posted on another forum. It ultimately means the effort invested by people on one forum is wasted...

It would therefor be polite to pro-actively mention the post has been cross-posted.

👍 1
4 years ago

Sorry about that, I didn't thought about it this way ... I'll delete my post on the other forum.

4 years ago Solution

Thanks for your help, I just solved it with that code:

TWIG
{% set random = random(page.collection({ items: '@self.siblings'}).published()) %}

Suggested topics

Topic Participants Replies Views Activity
Themes & Styling · by Pedro M, 2 months ago
4 224 2 months ago
Themes & Styling · by Ian, 2 months ago
3 118 2 months ago
Themes & Styling · by Norbert, 2 years ago
11 481 3 months ago
Themes & Styling · by Lukáš Findeis, 3 months ago
0 72 3 months ago
Themes & Styling · by Sebadamus, 4 months ago
5 152 3 months ago