Jump to content

Recommended Posts

Posted

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

Posted

Perhaps I'm getting this all wrong but is this what you are looking for?

foreach ($templates as $template) {
    echo $template->name;
}
Posted

I think you're getting it wrong, and Joss wants this instead:

foreach ($pages->find("template=$page->template") as $p) {
    echo $p->name;
}
  • Like 1
Posted

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

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

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.

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

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~

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...