Jump to:

3401 Posts in 979 Topics by 855 members

Template Questions

SilverStripe Forums » Template Questions » List all pages with specific page type

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 402 Views
  • Josiah
    Avatar
    Community Member
    7 Posts

    List all pages with specific page type Link to this post

    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?

  • zenmonkey
    Avatar
    Community Member
    524 Posts

    Re: List all pages with specific page type Link to this post

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

  • Josiah
    Avatar
    Community Member
    7 Posts

    Re: List all pages with specific page type Link to this post

    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?

  • zenmonkey
    Avatar
    Community Member
    524 Posts

    Re: List all pages with specific page type Link to this post

    Sorry my bad, it was late. This works

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

  • Josiah
    Avatar
    Community Member
    7 Posts

    Re: List all pages with specific page type Link to this post

    Works like a charm. Thanks!

    402 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.