Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Template Questions /

List all pages with specific page type


Reply


5 Posts   742 Views

Avatar
Josiah

Community Member, 7 Posts

15 October 2013 at 1:12pm

I need to be able to query every page with a specific page type, regardless of its position in the site tree.

Something like <% loop $ClassName == "MyPageType" %>

Is this possible?

Avatar
zenmonkey

Community Member, 528 Posts

15 October 2013 at 6:16pm

Just create a function in your Page.php like

function ListPagesByType($class) {
return $pages = $class::get()->count() ? $pages : false;
}

or if you don't like the terniary notation

function ListPagesByType($class) {
$pages = $class::get();
if ($pages->count()) {
return $pages;
} else {
return false;
}
}

Then all you need to do isloop

<% if ListPagesByType('MyClass') %>
<% loop ListPagesByType('MyClass') %><% end_loop %>
<% end_if %>

Avatar
Josiah

Community Member, 7 Posts

16 October 2013 at 7:02am

Edited: 16/10/2013 7:04am

Thanks for the response. However, it's not working for me.

Here's what I have in my page template:

<% if ListPagesByType('WorkPage') %>
   <% loop ListPagesByType('WorkPage') %>
      <li>$URLSegment</li>
   <% end_loop %>
<% end_if %>

Here's what I have in my Page class in Page.php:

function ListPagesByType($class) {
   return $pages = $class::get()->count() ? $pages : false;
}

Looks like it's not even meeting the condition because I tried putting in a <li> before the loop and it didn't show up either. Thoughts?

Avatar
zenmonkey

Community Member, 528 Posts

16 October 2013 at 7:19am

Sorry my bad, it was late. This works

function ListPagesByType($class) {
$pages = $class::get();
return $pages->count() ? $pages : false;
}

Avatar
Josiah

Community Member, 7 Posts

16 October 2013 at 7:27am

Works like a charm. Thanks!