Related Pages display loop

I’m trying to implement a related pages functionality, and I’ve added the blueprint, which seems to work as expected, but now when I go to display those related pages, it’s printing everything out twice?

Blueprint

  tab4:
    label: Related
    fields:
        related_pages:
          label: Related Pages
          type: pages

Panel

snippet from article.php

<?= $relatedPages = $page->related_pages()->toPages();
    foreach($relatedPages as $p):
       echo $p->title();
    endforeach;
?>

The text that is displayed on the frontend article page

higher-education-finance/literature-review-debt-and-returns-to-higher-education
guaranteed-income/the-urban-commonwealthLiterature Review: Debt and Returns to Higher EducationThe Urban Commonwealth

It seems like the initial variable $relatedPages is actually printing before the foreach loop. How can I prevent that from happening and just print the URL and title of each related page from the panel within the loop?

You are using an echo tag <?= right at the beginning of your code snippet, which doesn’t make sense. Correct code:

<?php $relatedPages = $page->related_pages()->toPages();
    foreach($relatedPages as $p):
       echo $p->title();
    endforeach;
?>
1 Like

Thank you!