Strange subpage behaviour

In order to have a collection of pages for legal purposes (Privacy Policy, Imprint etc.) I created a page that is unlisted legal and listed subpages privacy-policy imprint.

This way I’m able to create a dynamic menu for the footer. I followed the cookbook for submenus, this works all great.

Besides the fact that if I click on one of the links e.g. privacy-policy now - I get the 404 page with the privacy policy loaded below.

It doesn’t seem to be related to the unlisted parent page.

This is the menu I"m using in my footer snippet:

        if( !$page->is('legal') && page('legal')->hasListedChildren() ) :
          $items = page('legal')->children()->listed();
        elseif ( $page->hasListedChildren() ) :
          $items = $page->children()->listed();
        endif;

        if( $items and $items->isNotEmpty() ): 
        ?>
        <div class="footer__navigation">
          <nav>
            <ul class="navigation__list" >
              <?php foreach($items as $item): ?>
              <li class="navigation__item"><a href="<?= $item->url() ?>" alt="<?php $item->title()->html(); ?>" class="navigation__link"><?= $item->title()->html() ?></a></li>
              <?php endforeach; ?>
            </ul>
          </nav>
        </div>
      <?php endif;?>

There are a few issues with your code and your logic, I think:

  <?php
  if( $page->is('legal') && $page->hasListedChildren() ) :
          $items = page('legal')->children()->listed();
        elseif ( $page->hasListedChildren() ) :
          $items = $page->children()->listed();
        endif;

        if( isset($items) && $items->count() ): 
        ?>
        <div class="footer__navigation">
          <nav>
            <ul class="navigation__list" >
              <?php foreach($items as $item): ?>
              <li class="navigation__item"><a href="<?= $item->url() ?>" class="navigation__link"><?= $item->title()->html() ?></a></li>
              <?php endforeach; ?>
            </ul>
          </nav>
        </div>
      <?php endif;?>

Also please note the an a element doesn’t have an alt attribute.

Thanks for the reply. Almost there but still not working. This way I get all subpages of the current page in $items but I always need the children of legal:

if( $page->is('legal') && $page->hasListedChildren() ) :
          $items = $page->children()->listed();
        elseif( page('legal')->hasListedChildren() ):
          $items = page('legal')->children()->listed();
        endif;

and then I end up with the same behaviour.

And with the alt attribute, I shouldn’t work in the night :wink:

If you always want the children of legal, why the if else statement? Looks like I’m missing something here.

If you want the legal children on every page (which is what I would have expected)

  <?php
  if( ($p = page('legal')) && $p->hasListedChildren() ) :
          $items = $p->children()->listed();
  endif;

        if( isset($items) && $items->count() ): 
        ?>
        <div class="footer__navigation">
          <nav>
            <ul class="navigation__list" >
              <?php foreach($items as $item): ?>
              <li class="navigation__item"><a href="<?= $item->url() ?>" alt="<?php $item->title()->html(); ?>" class="navigation__link"><?= $item->title()->html() ?></a></li>
              <?php endforeach; ?>
            </ul>
          </nav>
        </div>
      <?php endif;?>

I actually added that, to avoid loading the legal page object on the legal page itself. Leaving it out before had the same results, I get the 404 Page with the subpage below it:

if( page('legal')->hasListedChildren() ) :
          $items = page('legal')->children()->listed();
        endif;

The 404 error is probably the result of another problem on those legal children pages themselves.

On a side note, please don’t use the code like this:

if( page(‘legal’)->hasListedChildren() ) :
$items = page(‘legal’)->children()->listed();
endif;

but like I posted above.

Yes, I know :wink: I used your code the above is just to lazyness while testing. Any Idea for debugging the 404?

Is debugging on?

What is in the template these children use?

Sure. The Default template.

Could you post the template these pages use?

Super Simple

<?php snippet('header') ?>

      <article class="main__content">
      <header class="article__header">
        <h2><a href="<?php echo $page->url(); ?>"><?php echo $page->title(); ?></a></h2>
        <time datetime="$page->date('c')"><?php echo $page->date('F dS, Y'); ?></time>
      </header>
        <?php echo $page->text()->kirbytext() ?>
      </article>

<?php snippet('footer') ?>

I also disabled my routes and barbar.js (ajax page loader) the behaviour consists. I just tried adding another subpage for another section, looks like I get the same results for all subpages, indipendent from the footer menu. It’s like the $page objetct gets returned twice.

It does not happen for the blog itself, I can open each article (which are at the end subpages) normally.

I have to digg why this is happening and where I hve the issue in my templates.

I also have the issue on the blog now :man_facepalming: Now everything is falling apart :smiley:

Great! That’s what you wanted, isn’t it?:wink:

Okay - its probably the route:

return [
    [
      'pattern' => '(:any)',
      'action'  => function($uid) {
        $page = page($uid);
        if(!$page) $page = page('blog/' . $uid);
        if(!$page) $page = site()->errorPage();
        return site()->visit($page);
      }
    ],
    [
      'pattern' => 'blog/(:any)',
      'action'  => function($uid) {
        go($uid);
      }
    ]
];

As I copied that blindly earlier when there was an issue with the home setting in an early version of Kirby 3 I don’t have a clue what it is actually doing. :smiley: The blog works if it is enabled, but the rest not.

Are those routes in your config or in a separate file? Because somehow there seems to be the route key missing?

Seperate file

Ok, but these routes look ok and should work without throwing errors on other pages. All the route does is make blog children work without the blog path in it.

Hm okay but then all other subpages that are not under blog do throw the error, right?

/blog/article <- works
/article <-works

/about <- works
/about/test <-breaks
/legal <- works
/legal/privacy-policy <-breaks

But these shouldn’t be affected by the route at all because the patterns don’t apply. The current patterns only listen to something like about, legal, blog etc. or blog/something.

Maybe subpages don’t work at all in your installation? Do they work if you remove the routes?