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?
This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.
Please use forum.silverstripe.org for any new questions
(announcement).
The forum archive will stick around, but will be read only.
You can also use our Slack channel
or StackOverflow to ask for help.
Check out our community overview for more options to contribute.
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?
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 %>
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?
Sorry my bad, it was late. This works
function ListPagesByType($class) {
$pages = $class::get();
return $pages->count() ? $pages : false;
}
Works like a charm. Thanks!