Jump to content

Finding Templates


Joss
 Share

Recommended Posts

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

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

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 :)

  • Like 1
Link to comment
Share on other sites

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

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

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

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