Joss Posted February 20, 2013 Share Posted February 20, 2013 Here is an interesting one. I know how to find pages based on templates, but can I find templates based on pages? Or perhaps a field in a page? The reason is that I want to list posts by template. Now, I could manually create a select field with a manual list of particular templates and use that to search, but that would mean manually updating that every time I (or the client/user) adds a template and it would not take into account whether the template is used or not. It would be a lot more interesting to create that list automatically based on what templates are actually being used by pages that are, for instance, beneath a certain parent. That way, the list is always up to date and does not display a template that is not actually being used by any published pages. So, is that possible? Joss Link to comment Share on other sites More sharing options...
arjen Posted February 20, 2013 Share Posted February 20, 2013 Perhaps I'm getting this all wrong but is this what you are looking for? foreach ($templates as $template) { echo $template->name; } Link to comment Share on other sites More sharing options...
diogo Posted February 20, 2013 Share Posted February 20, 2013 I think you're getting it wrong, and Joss wants this instead: foreach ($pages->find("template=$page->template") as $p) { echo $p->name; } 1 Link to comment Share on other sites More sharing options...
arjen Posted February 20, 2013 Share Posted February 20, 2013 I stand corrected! Link to comment Share on other sites More sharing options...
Joss Posted February 20, 2013 Author Share Posted February 20, 2013 Oh, goody! That means I can do something like $postpages = $pages->find("parent=/posts/"); $templates = $pages->find("template=$postpages->template..... or something like that to find all the templates used by a group of posts and then loop them out. Probably not quite right, but I haven't got that far yet Link to comment Share on other sites More sharing options...
neildaemond Posted February 20, 2013 Share Posted February 20, 2013 foreach($templates as $template){ $postsparent = $pages->get('/path/to/parent/'); $cwt = $postparent->children("template=$template->name"); if(count($cwt)){ echo "$template->name: \n"; foreach($cwt as $tp){ echo "\t $tp->name \n"; } } } i think this would list out the child pages of any parent grouped by which template they have. Not tested though, and not too sure if children("template=$template->name") is valid 1 Link to comment Share on other sites More sharing options...
Joss Posted February 20, 2013 Author Share Posted February 20, 2013 I was just wondering if I need to check with this whether the pages are visible or not - obviously I dont want to list a template if it doesn't have published pages. But then, I think the system should take that into account anyway. Link to comment Share on other sites More sharing options...
ryan Posted February 22, 2013 Share Posted February 22, 2013 i think this would list out the child pages of any parent grouped by which template they have. Not tested though, and not too sure if children("template=$template->name") is valid it is valid. Though you could also just specify $template (without the ->name). I was just wondering if I need to check with this whether the pages are visible or not - obviously I dont want to list a template if it doesn't have published pages. Any calls to a function that returns a PageArray (like $page->children(), $pages->find(), etc.) are going to exclude pages that are hidden, unpublished or otherwise inaccessible to the user. To make it include them, you can specify one of these: include=hidden -- includes hidden pages. check_access=0 -- Includes pages the user doesn't have view access, as a result of role permissions. include=all -- includes all pages with no restrictions. Link to comment Share on other sites More sharing options...
neildaemond Posted February 22, 2013 Share Posted February 22, 2013 Any calls to a function that returns a PageArray (like $page->children(), $pages->find(), etc.) are going to exclude pages that are hidden, unpublished or otherwise inaccessible to the user. To make it include them, you can specify one of these: include=hidden -- includes hidden pages. check_access=0 -- Includes pages the user doesn't have view access, as a result of role permissions. include=all -- includes all pages with no restrictions. thanks for that, I had this question in the back of my head too~ 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