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

Who are we?

We are a digital agency specialising in Web Design, Development, Concrete5 and digital marketing, based in London & West Sussex.

We make digital simple. Our purpose is to simplify your frustrations in digital and solve the challenges you face to help make you more money and progressively grow your business or organisation.

Tell me more

Keep up to date

June special offer...

FREE hosting in year one

We'll give you FREE hosting when you buy a website from us. The hosting includes full 24x7 UK support, FREE SSL cert, FREE domain, daily backups and is optimised for high levels of speed and security. Worth up to £480 a year!

Tell me more
Call us