DrMx Posted November 23, 2015 Posted November 23, 2015 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
DrMx Posted November 26, 2015 Author Posted November 26, 2015 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..
BitPoet Posted November 26, 2015 Posted November 26, 2015 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>'; } 3
DrMx Posted November 26, 2015 Author Posted November 26, 2015 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 =)
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