desbest Posted September 16, 2016 Share Posted September 16, 2016 Below code does not work. <?php $page = $pages->get("/home"); foreach ($page->children as $child) { echo "<a href='{$child->url}'>{$child->title}</a>"; } ?> But when I change /home to a different page, it does work. Am I not allowed to show the child pages of /home ? Link to comment Share on other sites More sharing options...
pwired Posted September 16, 2016 Share Posted September 16, 2016 For getting the home page it has to be like this: $pages->get("/"); 2 Link to comment Share on other sites More sharing options...
horst Posted September 16, 2016 Share Posted September 16, 2016 Try $p = $pages->get("/"); or $p = $pages->get("template=home"); or $p = $pages->get("name=home"); Please, better do not use $page = ..., instead use $p or some other varname. $page is a prepopulated API var from PW what holds the current page. 3 Link to comment Share on other sites More sharing options...
Mustafa-Online Posted September 16, 2016 Share Posted September 16, 2016 1 minute ago, desbest said: Why use twig when I can use php???!!! Twig = PHP .. just with less code / is there any way to use it with PW? Link to comment Share on other sites More sharing options...
LostKobrakai Posted September 16, 2016 Share Posted September 16, 2016 @Mustafa Online There are modules, which add twig into processwire and you could also just install it yourself e.g. via composer. For the future please open your own forum thread instead of seemingly highjacking other peoples'. Also simply searching for twig in the forums/google would probably have brought up the information you're seeking. 1 Link to comment Share on other sites More sharing options...
desbest Posted September 16, 2016 Author Share Posted September 16, 2016 Just now, Mustafa Online said: Twig = PHP .. just with less code / is there any way to use it with PW? If I learn twig I have to LEARN a new syntax but if I stick with php I use the syntax I am familiar with from making wordpress themes with their php templating language. I'm not a fan of people who try to make php shorter. Someone already tried to do that, then they scrapped the idea because it was a terrible idea. PHP will always be verbose. https://web.archive.org/web/20111202051225/http://getkirby.com/ Link to comment Share on other sites More sharing options...
Mustafa-Online Posted September 16, 2016 Share Posted September 16, 2016 @LostKobrakai - Thanx for reply Link to comment Share on other sites More sharing options...
qtguru Posted September 16, 2016 Share Posted September 16, 2016 2 hours ago, desbest said: Why use twig when I can use php???!!! Because plain PHP sucks and not easy to mix with code. TWIG compiles to PHP so nothing is missing. Link to comment Share on other sites More sharing options...
adrian Posted September 16, 2016 Share Posted September 16, 2016 3 hours ago, desbest said: Below code does not work. <?php $page = $pages->get("/home"); foreach ($page->children as $child) { echo "<a href='{$child->url}'>{$child->title}</a>"; } ?> But when I change /home to a different page, it does work. Am I not allowed to show the child pages of /home ? Also, for a less verbose version: foreach($pages->get('/')->children() as $child) { echo "<a href='{$child->url}'>{$child->title}</a>"; } No need to assign to a variable first if you're only using once. Of course if you're using it in other places then make it a meaningful variable like $homepage - at least anything other than $page because as @horst pointed out you will overwrite the object for the current page. 1 Link to comment Share on other sites More sharing options...
WillyC Posted September 18, 2016 Share Posted September 18, 2016 Quote Also, for a less verbose version: here.u go echo $pages("parent=/")->implode('<a href={url}>{title}</a> '); 5 Link to comment Share on other sites More sharing options...
adrian Posted September 18, 2016 Share Posted September 18, 2016 3 minutes ago, WillyC said: here.u go echo $pages("parent=/")->implode('<a href={url}>{title}</a> '); Yeah, yeah I do use implode(), explode(), and each() a lot, but never got used to the $pages(selector) shortcut to get(), but it is definitely nice! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now