Related content pagelist using array_intersect() to match topic(s) of current page with other news articles

David ReederBy David Reeder, 10 March 2026

In this post...

    What are we doing here?

    Imagine you are using topics in Concrete CMS and you simply want a page list that shows all news articles that share at least one topic with the current page.

    Pagelist block

    Add your pagelist block, entering 0 so it gets all News Article pages:

    pagelist-block.jpg

    Create a custom template

    Create a custom template in your /application/blocks/page_list/templates/ directory and apply the template to your page:

    1. Click on the block in edit mode and select "Design & Custom Template"
    2. Select it in the dropdown:
       select-custom-template.jpg

    Create an array of the current article topics

    Next, we need to create an array from our topics attribute and push in all the values from the attribute.

    $thisArticleTopics = $c->getAttribute('signals_topics');
    $curTopics = [];
    foreach($thisArticleTopics as $topic) {
        array_push($curTopics, $topic->getTreeNodeDisplayName());
    }

    Do the same for other articles

    We then do the same for the other articles, because we need an array, not a topic attribute object:

    $topics = $page->getAttribute('signals_topics');
    $arrTopics = [];
    foreach($topics as $topic) {
        array_push($arrTopics, $topic->getTreeNodeDisplayName());
    }

    We do this at the start of the foreach loop of pages, so we don't yet render any related articles.

    Filter, using array_intersect()

    Just before we write out our HTML for the other articles, we need to apply the array_intersect() method to find matches between our two topic arrays.

    It returns an array, listing the matches, so we just test to see if it's empty:

    if( !empty($result = array_intersect($curTopics, $arrTopics)) ) {
    ?>
        <article class="signals-card" data-aos="fade-up" data-aos-delay="<?= $delay ?>">
        ....

    Complete pagelist custom template

    Here is the complete custom template so you can see exactly where we added our code:

    <?php
    defined('C5_EXECUTE') or die("Access Denied.");
    
    $c = Page::getCurrentPage();
    
    /** @var \Concrete\Core\Utility\Service\Text $th */
    $th = Core::make('helper/text');
    /** @var \Concrete\Core\Localization\Service\Date $dh */
    $dh = Core::make('helper/date');
    
    ?>
    
    <div class="related-reading">
        <div class="container">
            <?php if (isset($pageListTitle) && $pageListTitle) { ?>
            <div class="text-center">
                <h2><?php echo $pageListTitle ?></h2>
            </div>
            <?php } ?>
    
            <div class="related-grid">
                    <?php
                            if ($c->isEditMode() && $controller->isBlockEmpty()) {
                                ?>
                                <div class="ccm-edit-mode-disabled-item"><?php echo t('Empty Page List Block.') ?></div>
                                <?php
                            } else {
    
                            $includeEntryText = false;
                            if (
                                (isset($includeName) && $includeName)
                                ||
                                (isset($includeDescription) && $includeDescription)
                                ||
                                (isset($useButtonForLink) && $useButtonForLink)
                            ) {
                                $includeEntryText = true;
                            }
                            
                            $thisArticleTopics = $c->getAttribute('signals_topics');
                            $curTopics = [];
                            foreach($thisArticleTopics as $topic) {
                                array_push($curTopics, $topic->getTreeNodeDisplayName());
                            }
                            
    
                            foreach ($pages as $page) {
                                $buttonClasses = 'ccm-block-page-list-read-more';
                                $entryClasses = 'blog-list';
                                $title = $page->getCollectionName();
                                if ($page->getCollectionPointerExternalLink() != '') {
                                    $url = $page->getCollectionPointerExternalLink();
                                    if ($page->openCollectionPointerExternalLinkInNewWindow()) {
                                        $target = '_blank';
                                    }
                                } else {
                                    $url = $page->getCollectionLink();
                                    $target = $page->getAttribute('nav_target');
                                }
                                $target = empty($target) ? '_self' : $target;
                                $thumbnail = false;
                                if ($displayThumbnail) {
                                    $thumbnail = $page->getAttribute('thumbnail');
                                }
                                if (is_object($thumbnail) && $includeEntryText) {
                                    $entryClasses = 'ccm-block-page-list-page-entry-horizontal';
                                }
                                $collectionDate = strtotime($page->getCollectionDatePublic());
                                $date = date("F j, Y", $collectionDate); 
    
                                $readingLength = $page->getAttribute('reading_length');
                                
                                $topics = $page->getAttribute('signals_topics');
                                $arrTopics = [];
                                foreach($topics as $topic) {
                                    array_push($arrTopics, $topic->getTreeNodeDisplayName());
                                }
        
                                $delay = 150;
                                
                                //$result = array_intersect($curTopics, $topics);
                                //print_r($result) .'<br />';
                                
                                if( !empty($result = array_intersect($curTopics, $arrTopics)) ) {
                                ?>
    
                                    <article class="signals-card" data-aos="fade-up" data-aos-delay="<?= $delay ?>">
                                        <a href="<?php echo h($url) ?>" class="signals-card-link">
                                            <div class="signals-card-image">
                                              <?php if (is_object($thumbnail)) {
                                                  // You need to create the thumbnail type below in the CMS...
                                                  $type = \Concrete\Core\File\Image\Thumbnail\Type\Type::getByHandle('blog_list');
                                                  $src = $thumbnail->getThumbnailURL($type->getBaseVersion());
                                                  echo "<img loading=\"lazy\" src=\"$src\" alt=\"{$thumbnail->getTitle()}\" width=\"{$type->getWidth()}\" height=\"{$type->getHeight()}\">";                                            
                                              } ?>
                                            </div>
                                            <div class="signals-card-content">
                                              <?php
                                                    foreach($arrTopics as $topic) {
                                                        echo '<div class="signals-card-category">';
                                                        echo $topic . ' ';
                                                        echo '</div> ';
                                                    }
                                                  ?>
                                              <h3><?php echo h($title) ?></h3>
                                              <div class="signals-meta">
                                                <span class="signals-date"><?php echo h($date) ?></span>
                                                <span class="signals-divider">•</span>
                                                <span class="signals-readtime"><?= $readingLength ?>  min read</span>
                                              </div>
                                              <span class="signals-card-arrow">→</span>
                                            </div>
                                        </a>
                                    </article>
                                    <?php 
                                } // filter list using array_intersect()
                            } // foreach ?>             
    
                            <?php if (count($pages) == 0) { ?>
                                <div class="ccm-block-page-list-no-pages"><?php echo h($noResultsMessage) ?></div>
                            <?php } ?>
    
                        <?php
                    } ?>
            </div>
        </div>
    </div>


    Back