Jump to content

How can i list child pages regardless of his parents?


DrMx
 Share

Recommended Posts

Hi

I want to organize my page tree as home > category > subcategory > post and already made a separate tags page (all fine in that)

Categories may have multiple subcategories in itself. And i want to organize this way for a better visual understanding of where to make every post entry.

Problem is i want to list posts by date related at other posts level, and i can only order posts under their parents.. Don't know if i make sense.

I had attached an image: Left side is my page tree at PW admin, center is the tree structure that i get with the first code (which i can use in another page, but not home) and right is what i want to accomplish.

This is my home.php code for the tree structure (respecting parents)

<?php
	foreach ($page->find('parent=news') as $cat) {
		echo 'parent page<br/>';
		foreach ($cat->children('sort=-published') as $new) {
			echo $new->title.'<br/>';
		}
	}
?>

And i made this code using templates, each post having a template related to category. This second example works fine, but i want to know if there's another direct way to accomplish this without the need to create a template for each category?

<?php 
	foreach ($page->find('template=news, sort=-published') as $item) {
		echo $item->title.'<br/>';
	}
?>

Thanks in advance, i have two months aprox learning Processwire and has been great even if i didn't understand everything =p

post-3658-0-48890100-1448258007_thumb.jp

Link to comment
Share on other sites

I just found the answer as:

<?php
	foreach ($page->find('parent=/category/subcat1/|/category/subcat2/, sort=-published') as $new) {
		echo $new->title.'<br>';
	}
?>

I suppose that it was a silly question after all.. As you can see i'm not an expert programmer but i'm trying =)

Still i'm wondering if i can improve the /category/ part of the code..

Link to comment
Share on other sites

All the selector possibilities can be quite a handful to wrap your head around  :)  One nice thing about PageArrays is that they stringify into a pipe-delimitied list of the ids of its contained pages perfect for a selector expression, so you could generalize your code to:

$categories = wire('pages')->get('/news/')->children();
foreach( wire('pages')->find("parent=$categories, sort=-published") as $news ) {
    echo $news->title . '<br>';
}
  • Like 3
Link to comment
Share on other sites

Wow that seem a better solution thanks a lot, can i find info related to that concept somewhere or is experience knowledge?

I was struggling to match the subcategory with a tag, but i just found the answer =)

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