Structure field related pages

Hello

I’m trying to do a related pages system like the cookbook explains https://getkirby.com/docs/cookbook/related-articles. I tried to do it with structure fields but I have an error that say : Invalid argument supplied for foreach()
This is my foreach

<?php foreach($page->related()->toStructure() as $item): ?>
        <li>
            <a href="<?php echo $item->url() ?>">
                <?php echo $item->title()->html() ?>
            </a>
        </li>
<?php endforeach ?> 

and my blueprint is like that

  related:
    label: Related
    type: structure
    entry: >
      {{pages}}
    fields:
      pages:
        label: Pages
        type: page

I tried several things but I can’t make it works. Any ideas on how to solve it would be appreciated.

1 Like

I can’t see anything wrong with the foreach loop as such, but you are not getting the pages:

    <?php foreach($page->related()->toStructure() as $item):
      $p= $item->pages()->toPage();
      if($p):
     ?>
       <li>
         <a href="<?php echo $p->url() ?>">
           <?php echo $p->title()->html() ?>
         </a>
       </li>
      <?php endif ?>
    <?php endforeach ?>

Oh thank you but it does exactly the same error message : Invalid argument supplied for foreach()
(I’m on Kirby 2.5.1)

my .txt looks like that …

Related: 

- 
  pages: projects/project-b
- 
  pages: projects/project-c

Does the error refer to that foreach loop at all?

Where are you using this foreach loop and which page are you referring to?

I’m using the foreach loop into the “project.php” template

–> site/templates/project.php

the related pages are on projects/project-b and projects/project-c

This seem to be logical for me no ?

Yes, that’s a bit weird, could you please post the complete project.php template?

Yes, my template is only at the very beginning of the development. The only buggy lines are the foreach for the related pages…

<?php snippet('header') ?>

<main class="main" role="main">

    <h1><?php echo $page->title()->html() ?></h1>

    <time datetime="<?php echo $page->date('c') ?>"><?php echo $page->date('Y', 'year') ?></time>,
    <?php echo $page->materiaux() ?>,
    <?php echo $page->dimensions() ?>

    <div class="text">
        <?php echo $page->text()->kirbytext() ?>

        <?php foreach($page->images()->sortBy('sort', 'asc') as $image): ?>
        <figure>
            <img src="<?php echo $image->url() ?>" alt="<?php echo $page->title()->html() ?>">
        </figure>
        <?php endforeach ?>
    </div>

    <ul>
        <?php foreach($page->related()->toStructure() as $item):
                 $page = $item->pages()->toPage();
                 if($page):
        ?>
        <li>
            <a href="<?php echo page($item->pages())->url() ?>">
                <?php echo page($item->pages())->title()->html() ?>
            </a>
        </li>
        <?php endif ?>
        <?php endforeach ?>
    </ul>

    <nav class="nextprev cf" role="navigation">
        <?php if($prev = $page->prevVisible()): ?>
        <a class="prev" href="<?php echo $prev->url() ?>">&larr; previous</a>
        <?php endif ?>
        <?php if($next = $page->nextVisible()): ?>
        <a class="next" href="<?php echo $next->url() ?>">next &rarr;</a>
        <?php endif ?>
    </nav>

</main>

<?php snippet('footer') ?>

I don’t know, I did exactly what you suggested above. It’s a very basic and minimal Kirby layout.
It could be cool to provide a zip to someone just to check.

The problem is due to the fact that you use the related pages plugin which provides a page method called related() that interferes with the naming of your related field.

Options:

  • delete plugin if you don’t need it or comment the page method
  • rename your field to something else
1 Like

Oh shit I forgot this plugin. This was my first test to get related pages. Thank you very much Tenixe !
The kirby-related-ages plugin is good to have a system based on tags but I wanted something more manual.

Thank you all !