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

Find and get a page by its slug or path

Solved by pamtbaau View solution

Started by Seb 8 years ago · 2 replies · 1423 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 Slebeig, 5 days ago
1 103 5 days ago
Plugins · by Rene, 4 weeks ago
2 303 4 weeks ago
Plugins · by Xavier, 2 months ago
2 226 1 month ago
Plugins · by Luka Prinčič, 7 years ago
3 1413 2 months ago
Plugins · by Sebastian van de Meer, 2 months ago
1 322 2 months ago