How can I omit a folder in a breadcrumb?


#1

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


#2

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?


#3

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.


#4

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.


#5

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


#6

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


#7

Now I understand it, thank you.


#8

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; ?>

#9

:ok_hand: