Top Lines

Add Pagelist Programatically in Concrete5

by Dave Reeder , 15 August 2018

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

Join the Discussion...


Back to Blog

Get Important News & Offers

Enter your email to receive monthly news and special offers from Made Simple Media:

You can unsubscribe at any time, please read our privacy policy for more information.

Get a FREE Quote...

Contact our friendly team for a FREE no-obligation quote...

Call now on 0800 111 4504

Stay Informed...

Enter your email address to receive occassional emails with special offers, relevant news and useful tips:

Please note, we never share your data with any third party and you can unsubscribe from emails at any time and request that your data is deleted.