Skip to content
Grav 2.0 is officially stable. Read the announcement →
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 · 643 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

@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.

Suggested topics

Topic Participants Replies Views Activity
Themes & Styling · by stuart young, 2 weeks ago
3 251 2 weeks ago
Themes & Styling · by Sebadamus, 3 weeks ago
5 317 3 weeks ago
Themes & Styling · by martynfoster735, 4 weeks ago
1 310 4 weeks ago
Themes & Styling · by Justin Young, 1 month ago
1 348 1 month ago
Themes & Styling · by Slebeig, 2 months ago
4 474 2 months ago