Add Pagelist Programatically in Concrete5

by Dave Reeder , 15 August 2018

This code is for Concrete5 v5.7+.
To embed the pagelist, first load the navigation helper:
$nh = Loader::helper('navigation');
Then we add the following, notice in this case we are filtering the results so that we show blog pages - i.e. pages which use the blog_page pagetype:
$pl = new \Concrete\Core\Page\PageList(); $pl->filterByPageTypeHandle('blog_page'); $pages = $pl->getResults();
Then we need a foreach loop to go through the results and actually write out pages:
foreach($pages as $page) { $title = $page->getCollectionName(); $intro = htmlentities($page->getCollectionDescription()); $url = $nh->getCollectionURL($page); echo $title; }
In this example, we are just writing out the title of each page.
Complete Pagelist Example
So our complete pagelist example looks like this:
$nh = Loader::helper('navigation'); $pl = new \Concrete\Core\Page\PageList(); $pl->filterByPageTypeHandle('blog_page'); $pages = $pl->getResults(); foreach($pages as $page) { $title = $page->getCollectionName(); $intro = htmlentities($page->getCollectionDescription()); $url = $nh->getCollectionURL($page); echo $title; }
Easy isn't it?
Of course, you can do much more. Here are some of the filters available:
$pl->filterByPageTypeHandle('blog_page'); $pl->filterByPath('/recent-work', true); $pl->filterByKeywords();
See more filters at /concrete/src/Page/PageList.php
Adjustments for v5.6+
For version 5.6 of Concrete5 you want to adjust the above code to have the following lines:
Loader::model('page_list'); $pl = new PageList(); $pages = $pl->get();
Join the Discussion...