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.

Plugins

Find and get a page by its slug or path

Solved by pamtbaau View solution

Started by Seb 8 years ago · 2 replies · 1336 views
8 years ago

Hi!

Sorry for my newbie question but I spent hours to get this working:

Within my plugin, I want to get an already created Page by its slug or path?

Like $found = $this->grav->pages->get('myslug')

I am sure it is easy but I can't find my way...

Thanks!

8 years ago Solution

@Amatiq Have a look at the docs about \Grav\Common\Page\Pages and Plugin Event Hooks

In event, or after, onPagesInitialized, you could try the following two options:

PHP
$pages = $this->grav['pages'];
$page = $pages->find('/blog/hero-classes');
$page = $pages->get('/path/to-root/user/pages/01.blog/hero-classes');

And, of course, depending on what you want to do and where you are, there are probably more roads that lead to Rome...

👍 1
last edited 08/23/18 by pamtbaau
8 years ago

You are right! I asked for pages too early...

I must ask for pages in onPageInitialized()

Done!

👍 1

Suggested topics

Topic Participants Replies Views Activity
Plugins · by Rene, 1 week ago
2 47 1 week ago
Plugins · by Xavier, 4 weeks ago
2 56 4 weeks ago
Plugins · by Luka Prinčič, 7 years ago
3 1183 1 month ago
Plugins · by Sebastian van de Meer, 1 month ago
1 50 1 month ago
Plugins · by PIERROT Alain, 2 months ago
3 74 2 months ago