Exclude home from Breadcrumbs. Treat next page as parent

How can I exclude the $site() I call this the homepage then I want to exclude the next page and then every page after that I want to show everypage but $site();

For example. N

ow I have - If site() {nothing} else {show breadcrumb}
This returns nothing on the homepage
but on the other pages returns this. Home > Otherpage > OtherSubPage > AnotherPage.

What I want to do is.

Current Script Shows :
Home > Otherpage > OtherSubPage > AnotherPage.

New Script
If user visits Otherpage
Show Nothing

If user visits OtherSubpage
Otherpage > OtherSubPage

If user visits AnotherPage
Otherpage > OtherSubPage > Another Page

Current breadcrumb script.

<nav class="breadcrumb" role="navigation">
  <ul>
    <?php foreach($site->breadcrumb() as $crumb): ?>
    <li>
      <a href="<?php echo $crumb->url() ?>">
        <?php echo html($crumb->title()) ?>
      </a>
    </li>
    <?php endforeach ?>
  </ul>
</nav>

Almost got it please don’t reply just yet.

If you have a look at the breadcrumb method, you will see that it simply fetches the parents and then add the home page to it. So you just have to remove the homepage again.

  public function breadcrumb() {

    if(isset($this->cache['breadcrumb'])) return $this->cache['breadcrumb'];

    // get all parents and flip the order
    $crumb = $this->page()->parents()->flip();

    // add the home page
    $crumb->prepend($this->homePage()->uri(), $this->homePage());

    // add the active page
    $crumb->append($this->page()->uri(), $this->page());

    return $this->cache['breadcrumb'] = $crumb;

  }

Like this:

  <nav class="breadcrumb" role="navigation">
    <ul>
      <?php foreach($site->breadcrumb()->not('home') as $crumb): ?>
      <li>
        <a href="<?php echo $crumb->url() ?>">
          <?php echo html($crumb->title()) ?>
        </a>
      </li>
      <?php endforeach ?>
    </ul>
  </nav>
1 Like

If you are likely to use this more often (including future projects), I’d create a custom site method for reuse:

site::$methods['crumbs'] = function($site) {

    // get all parents and flip the order
    $crumb = $site->page()->parents()->flip();

    // add the active page
    $crumb->append($site->page()->uri(), $site->page());

    return $crumb;
};

Maybe this is not the place to put this, but Kirby has the best support of any CMS out there.

4 Likes