Jump to content

Find pages, display children per page


Martinus
 Share

Recommended Posts

I have this selector:

$items = $pages->find("parent=/categories/, sort=title");
                foreach($items as $item) { 
                echo

<h4><a href=''>" . $item->title . "</a></h4> 
<ul>" . $item->children->each( "<li><a href='{url}'>{title}</a></li>" ) . "</ul>

But, do I need to check with if($item->hasChildren ? Because the IF does not work.

Link to comment
Share on other sites

  • Martinus changed the title to Find pages, display children per page

Check this :

https://processwire.com/api/ref/page/children/

// Render navigation for all child pages below this one
foreach($page->children() as $child) {
  echo "<li><a href='$child->url'>$child->title</a></li>";
}

So in your case:

$children = $pages->find("parent=/categories/, sort=title")->children();
                foreach($children as $child) { 
				// ... manipulate $child here                
}

 

  • Like 1
Link to comment
Share on other sites

I understand if I want to find children of the page I am on I can do that. The API says:

// Render navigation for all child pages below this one

 But in my case I am on one page and finding category pages (elsewhere) - then I display them one by one as $item:

foreach($items as $item) {

So now I have parent $item-1 and need it's children; I have parent $item-2 and need it's children; etc.

$item->children->each( "<li><a href='{url}'>{title}</a></li>" )

works fine.... I think.

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