Top Lines

Add Pagelist Programatically in Concrete5

by David Reeder, 15 August 2018

Dave Reeder
Share
Add Pagelist Programatically in Concrete5
There are many reasons why you may want to hard-code a pagelist rather than use a block. The most common reason I find is when I want to make an ajax pagelist. I will hard-code the pagelist into a tools file and call that tools file from my javascript ajax function.

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();


Back to Blog

Ask a Question...

Please complete the form below to send us a message: