Skip to content

Instantly share code, notes, and snippets.

@AndrewHaine
Created May 18, 2022 13:13
Show Gist options
  • Save AndrewHaine/750020cf4fbf07b39b398960249980de to your computer and use it in GitHub Desktop.
Save AndrewHaine/750020cf4fbf07b39b398960249980de to your computer and use it in GitHub Desktop.
<?php
namespace App\Tags;
use Statamic\Tags\Tags;
class ParentCategoryBreadcrumbs extends Tags
{
/**
* The {{ parent_category_breadcrumbs }} tag.
*
* {{ parent_category_breadcrumbs }}
* <li>
* {{ if is_current }}
* {{ crumb:title }}
* {{ else }}
* <a href="{{ crumb:url }}">{{ crumb:title }}</a>
* {{ /if }}
* </li>
* {{ /parent_category_breadcrumbs }}
*
* @return string|array
*/
public function categories()
{
$current = [
'is_current' => true,
'crumb' => $this->context->get('page'),
];
$parents = [];
$parent = $this->context->get('parent_category')?->value();
while ($parent) {
$parents[] = [
'is_current' => false,
'crumb' => $parent,
];
$parent = $parent->parent_category;
}
$breadcrumbs = \array_reverse([$current, ...$parents]);
return $this->parseLoop($breadcrumbs);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment