Breadcrumbs with CustomPostType

Even after a long search, I couldn’t find a solution here. So I have to ask…

I wrote a CPT (not a plugin) that displays categories and their subcategories hierarchically. The path is displayed correctly in the address bar.

Example:
https://example.com/cpt-something/first-category/second-category/the-post/

Now I would like to use Bricks breadcrumbs. Unfortunately I don’t understand how it works. The path is not as I had hoped (the categories are missing):
https://example.com/cpt-something/the-post/

What can I do here?

1 Like

I’m also having this issue - the breadcrumb element completely misses out the taxonomy and CPT structure having just Home > CPT Post rather than Home > CPT Archive > CPT Taxonomy > CPT Post

Any guidance would be appreciated from the Bricks team! Thanks!

1 Like

I am having the same problem. Would appreciate guidance @timmse

Anyone figure this out??

here
it works well on singles
test it more on other pages

function custom_breadcrumbs() {
    // Start of the breadcrumb list
    echo '<ul class="breadcrumbs">';
    echo '<li><a href="' . get_home_url() . '">Home</a></li>';

    // Check if the current view is a category, single post, taxonomy, or archive page
    if (is_category() || is_single() || is_tax() || is_archive()) {
        $post_type = get_post_type();

        // If the post type is not a standard post, display its type in the breadcrumb
        if ($post_type != 'post') {
            $post_type_object = get_post_type_object($post_type);
            $post_type_archive = get_post_type_archive_link($post_type);
            echo '<li><a href="' . $post_type_archive . '">' . $post_type_object->labels->singular_name . '</a></li>';
        }

        // If viewing a single post, display its categories in the breadcrumb
        if (is_single()) {
            $categories = get_the_category();
            if ($categories) {
                foreach ($categories as $category) {
                    echo '<li><a href="' . get_category_link($category->term_id) . '">' . $category->name . '</a></li>';
                }
            }
            // Display the current post title
            echo '<li>' . get_the_title() . '</li>';
        } elseif (is_tax()) {
            // If viewing a taxonomy, display the current term
            $current_term = get_queried_object();
            echo '<li><a href="' . get_term_link($current_term) . '">' . $current_term->name . '</a></li>';
        } elseif (is_archive()) {
            // If viewing an archive, display the archive title
            $archive_title = post_type_archive_title('', false);
            echo '<li>' . $archive_title . '</li>';
        }
    } elseif (is_page()) {
        // If viewing a page, display the page title
        echo '<li>' . get_the_title() . '</li>';
    }

    // End of the breadcrumb list
    echo '</ul>';
}

// Function call to generate the breadcrumbs
custom_breadcrumbs();

I am having the exact issue with Metabox CPT and custom taxonomies. Did you figure it out?

I think it’s a bug. In the editor, it looks correct, but on the front-end, it misses an item.

It works correctly on the Type: Single templates, but when the template is set to Archive, it doesn’t work correctly, no matter what I do…

What is worse is that it works in the Bricks Editor, it even appears to be working fine when you enter Bricks Preview mode, but on the actual FrontEnd - nope!

Unfortunately, I have not yet been able to solve the problem with bricks alone. “Rank Math” is my friend here - it gives me the breadcrumb I want. You can take a look at this page as an example: Platin, das Element - Geschichte, Vorkommen, Verwendung

I found a possible solution for the @nettl-td issue of the missing post type archive link, when viewing taxonomy archive.

Seems like at bricks theme ‘includes/elements/breadcrumbs.php’ line 388 ‘$show_archive’ is done to recieve post archive item, but this part is skipped for ‘is_category()’ at line 475. I am not sure if it’s a bug or an intended feature.

Anyway, digging trough the file I found an undocumented filter :slight_smile:
‘bricks/breadcrumbs/items’

Using it I could add additional breadcrumbs item for my default blog post archives/taxonomies:

add_filter( 'bricks/breadcrumbs/items', function( $breadcrumb_items ) {
    // Check if we are on a category archive page
    if ( is_category() ) {
        // Get the post type object for "post" (or replace with your custom post type)
        $post_type = get_post_type_object( 'post' );

        if ( $post_type ) {
            // Generate the post type archive link
            $post_type_link  = get_post_type_archive_link( $post_type->name );
            $post_type_label = $post_type->labels->name ?? $post_type->label;

            // Insert the post type archive link at the second position (after "Home")
            array_splice( $breadcrumb_items, 1, 0, sprintf(
                '<a class="item" href="%s">%s</a>',
                esc_url( $post_type_link ),
                esc_html( $post_type_label )
            ));
        }
    }

    return $breadcrumb_items;
});

This should work for CPT and other stuff too, just change the code accordingly. It let’s you to add completelly custom breadcrumbs items too :smiley:

Dunno if this filter is up to stay for long, but it works at current Bricks version (2.0 alpha).

1 Like