Jump to content

Remove Post Title item from Breadcrumb trail on Archive/Search pages


Recommended Posts

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!

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.

@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 by Devon808

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.

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.

  • Like 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.