Devon808 Posted June 21, 2022 Share Posted June 21, 2022 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! Quote Link to comment https://forums.phpfreaks.com/topic/314951-remove-post-title-item-from-breadcrumb-trail-on-archivesearch-pages/ Share on other sites More sharing options...
requinix Posted June 21, 2022 Share Posted June 21, 2022 Working backwards, you can see the breadcrumbs come from $items inside wrap(), which is called by render(). Inside that is a "breadcrumbs" which starts with "categories" and then will prepend the "bloginfo" as well as push (append) the $post. So try removing the bit to do with the push. That line also says "active" so you'll probably want to put that on the last item in the breadcrumb list, but honestly that's probably something better suited for CSS. Quote Link to comment https://forums.phpfreaks.com/topic/314951-remove-post-title-item-from-breadcrumb-trail-on-archivesearch-pages/#findComment-1597522 Share on other sites More sharing options...
Devon808 Posted June 21, 2022 Author Share Posted June 21, 2022 (edited) @requinixThanks for replying. I still need the post title to render when viewing a post. Doesn't seem necessary, but that is what I am tasked with. The goal is to make this code work for other page/post types but omitting the post title for archive and search pages. Edited June 21, 2022 by Devon808 Quote Link to comment https://forums.phpfreaks.com/topic/314951-remove-post-title-item-from-breadcrumb-trail-on-archivesearch-pages/#findComment-1597523 Share on other sites More sharing options...
requinix Posted June 22, 2022 Share Posted June 22, 2022 Oh, this is being used by the other pages too? Add a parameter to the function about whether it should include the post's title at the end, default it to true for convenience, then modify the archive and search pages to pass false instead. Quote Link to comment https://forums.phpfreaks.com/topic/314951-remove-post-title-item-from-breadcrumb-trail-on-archivesearch-pages/#findComment-1597536 Share on other sites More sharing options...
Devon808 Posted June 22, 2022 Author Share Posted June 22, 2022 @requinix can I get a lil assistance on what that might look like? Quote Link to comment https://forums.phpfreaks.com/topic/314951-remove-post-title-item-from-breadcrumb-trail-on-archivesearch-pages/#findComment-1597555 Share on other sites More sharing options...
Devon808 Posted June 28, 2022 Author Share Posted June 28, 2022 (edited) Bumping this to see how that parameter would look? Apologies as I am not at in a position to interpret this yet. Edited June 28, 2022 by Devon808 Quote Link to comment https://forums.phpfreaks.com/topic/314951-remove-post-title-item-from-breadcrumb-trail-on-archivesearch-pages/#findComment-1597662 Share on other sites More sharing options...
requinix Posted June 28, 2022 Share Posted June 28, 2022 An optional parameter would be like public static function render($post, $withPost = true) { which will default to true if not provided. You would then use $withPost to decide whether to do that ->push or not. 1 Quote Link to comment https://forums.phpfreaks.com/topic/314951-remove-post-title-item-from-breadcrumb-trail-on-archivesearch-pages/#findComment-1597675 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.