Nav ForLoop Snippet Changing Page Information

I’m trying to create a subnav header for a specific page template. However, when I run a forloop to create the main nav

        <nav>
        <?php if(!$page->isHomePage()): ?> 
            <a href="<?= $site->url() ?>"> Home </a> 
        <?php endif ?>
        <?php 
        $pages = $site->children()->listed();
        foreach($pages as $page): ?>
        <a href="<?= $page->url() ?>"> <?php echo $page->title() ?> </a>
        <?php endforeach ?>
        </nav>     

This spits out:

  1. Home
  2. Contributions
  3. Resources
  4. About

Now, any attempt to access the specific page information in the code after the nav like so:

            </nav>     
            <?= $page->url() ?>

Is only giving me the URL and template information of the ‘about’ page like: https://localhost/critical-code-cookbook/about

However, when I put the code before the nav like so:

<?= $page->url() ?>
            <nav>

This gives me the actual page’s information: https://localhost/critical-code-cookbook/contributions/house-of-words

I’ve attached an image below showcasing the issue.

Is there something wrong with my setup for the navigation?

Never mind, I just realized my issue. I was changing the $page variable in the forloop. :man_facepalming:

It’s fixed though I don’t mind leaving this up in case someone else makes this mistake.

Exactly, Kirby has some default variables like $kirby, $site, $pages and $page that you shouldn’t overwrite with your own definitions to prevent such issues.