How can I omit a folder in a breadcrumb?

I use this code to create a breadcrumb:

    <nav class="breadcrumb">

        <?php foreach($site->breadcrumb() as $crumb): ?>
            <a class="breadcrumb-item" href="<?= $crumb->url() ?>">
                <span class="breadcrumb-label"><?= $crumb->title()->html() ?></span>
            </a>
        <?php endforeach; ?>

    </nav>

It is possible to omit a folder and show its subfolders?

Example:

breadcrumb

Hm, a breadcrumb shows a hierarchy, but Page C in your example is not a descendant of Page B, or is it?

You could use an if-statement I guess, to skip the folder. Do you want to do that for a particular folder only?

Yes, they are descendants, there are 4 levels.

I omitted the folder in the URL and now I’d like to omit it also in the breadcrumb.

Replace name of page with the one you want to skip:

  <nav class="breadcrumb">
    
    <?php foreach($site->breadcrumb() as $crumb): ?>
      <?php if($crumb->is(page('blog'))) continue; ?>
      <a class="breadcrumb-item" href="<?= $crumb->url() ?>">
        <span class="breadcrumb-label"><?= $crumb->title()->html() ?></span>
      </a>
    <?php endforeach; ?>
    
  </nav>

Edit: this completely removes the blog page from the breadcrumb navigation, though, even if the blog page is active.

Perfect! Can you explain me a little bit the logic of how it works?

It tests within the if-statement if the crumb-page is the same as another page (the blog page) and if that’s the case, continues with the next loop, skipping the rest of the code in the loop for that item.

http://php.net/manual/en/control-structures.continue.php

1 Like

Now I understand it, thank you.

If we want to show the blog crumb if the blog page is active, we need another condition:

<?php if($crumb->is(page('blog')) && !page('blog')->isActive()) continue; ?>

:ok_hand: