How works $pages->has($page)?

Hi everyone!
I’m trying to show a button only if a subpage exist and I thought that $pages->has($page) will work for me, so I followed the documentation but is not working as I expected.
I’ve printed $page->children() and I’ve found that the collection is made of the subpages paths. Is this correct?

Thanks in advance!

<?php if($page->children()->has('details')): ?>
      <li>
          <a class="button" href="<?php echo $page->url()?>/details">details</a>
      </li>
<?php endif ?>

This is either a bug or the example is wrong. $pages->has() seems to work only on the pages level, i.e. pages of the first level, not on the children level. Need to correct this in the docs.

So, the correct code to know if the subpage exist should be:

<?php if($page>has('details')): ?>
      <li>
          <a class="button" href="<?php echo $page->url()?>/details">details</a>
      </li>
<?php endif ?>

This is return me always true, even when the page is not present.

No, no, no :wink:, as I said, it only works on the $pages collection, $pages includes all pages of the first level in content, in the starterkit that would be “about”, “projects” etc. The correct code would be:

$pages->has('about');

But that won’t help you any if you want to check for a child page.

oh ok, thanks! I will find another way.

I just found that it works if you pass the parent page:

<?php echo $page->children()->has('parent_page/details') ?>

Yes I thought it too, but this is not really dynamic to re use it on the pages.
Should I pass it the parent page as variable? and concatenate the strings?
Thanks.

I think it’s solved. Thanks for your input!

<?php if($page->children()->has($page->uri().'/details')): ?>
     <li>
	 <a class="button" href="<?php echo $page->url()?>/details">details</a>
     </li>
<?php endif ?>

There is an issue on GitHub about this now and this will be fixed in the future so that your temporary solution will not be required anymore then. :slight_smile:

1 Like

This has now been fixed on the development branch and will be available in the next release.