Jump to content

Why can't I get the child pages of /home ?


desbest
 Share

Recommended Posts

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

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.

  • Like 3
Link to comment
Share on other sites

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

  • Like 1
Link to comment
Share on other sites

Just now, Mustafa Online said:

Twig = PHP .. just with less code / is there any way to use it with PW? :undecided:

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

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.

  • Like 1
Link to comment
Share on other sites

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...