I have someone else's code to update. My understanding of this logic is in progress, I'm more frontend - could use some help.
I would like to programmatically remove the Post Title item from the Breadcrumb trail for archive and search pages.
Otherwise, the last item in the trail. No CSS or JS, please.
Current Results:
Home > Blog > Category > Post Title
The post title comes from the first item in the list of posts for the active category.
Desired Results:
Home > Blog > Category
The code looks to be using Tightenco Collect package v8.0, but quite old as the github blame shows the last update was in 2019.
<?php namespace Base;
/**
* Renders the breadcrumb trail
*/
class BreadcrumbTrail {
public static function render($post) {
$categories = collect(wp_get_post_terms($post->ID, ['category']))->sortBy('parent');
$breadcrumbs = $categories
->map(function ($term) {
return self::render_item(get_category_link($term->term_id), $term->name);
})
->prepend(self::render_item(get_bloginfo('url'), get_bloginfo('name')))
->push(self::render_item(get_permalink($post->ID), $post->post_title, 'active'));
return self::wrap($breadcrumbs->implode(''));
}
public static function render_item($url, $title, $active = '') {
$item = "<li class='breadcrumb-item %s'><a href='%s'>%s</a></li>";
return sprintf($item, $active, $url, $title);
}
public static function wrap($items) {
ob_start();
?>
<div class="breadcrumb-container">
<ol class="breadcrumb-list">
<li class='breadcrumb-item'><a href='https://mydomain.com'>Home</a></li>
<?php echo $items; ?>
</ol>
</div>
</div>
<?php
return ob_get_clean();
}
}
A single header.php file is used for all pages with conditions set for specified page types.
The class is echoed in the header.php file
<?php echo \Base\BreadcrumbTrail::render(get_post()): ?>
Thanks!