How to change breadcrumb’s to display URLs

#422569
  • Resolved Hugo Alais
    Rank Math pro

    Hi there,
    Currently my breadcrumb’s display the title of each page, when I want them to display the URL structure. How do I do this?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hello,

    Thank you for contacting Rank Math and bringing your concern to our attention. I’m sorry for any inconvenience this issue may have caused you.

    You can use and customize this filter to echo the slug as the anchor text for breadcrumbs instead of the title:

    add_filter( 'rank_math/frontend/breadcrumb/items', function( $crumbs, $class ) {
        if(is_singular('post')){
            global $post;
            $page_slug = $post->post_name;
            $crumbs[count($crumbs)-1][0] = $page_slug;
        }
        return $crumbs;
    }, 10, 2);

    The filter should be added to your active theme’s functions.php file. Here’s another way to apply the filter using the rankmath.php file:
    https://rankmath.com/kb/filters-hooks-api-developer/#adding-filters-and-hooks-in-rank-math-php

    I hope that helps.

    Thank you.

    Hi Reinelle,
    Is this for both posts and pages? and do I need to replace anything?

    Hello,

    The current filter my colleague presented only works on posts. If you wish to apply this on pages, you can insert this in the condition statement:

    is_page()

    If you wish to use your URL slug as breadcrumbs item, you don’t need to change anything.

    Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.

    Hey Jeremy,
    So what’s a conditions statement? I do not code. Secondly my breadcrumbs are currently not my URL slug, they are the title of the page!

    Hello,

    Kindly try applying this modified filter code to your theme’s functions.php file:

    add_filter( 'rank_math/frontend/breadcrumb/items', function( $crumbs, $class ) {
        if(is_singular('post') && is_page()){
            global $post;
            $page_slug = $post->post_name;
            $crumbs[count($crumbs)-1][0] = $page_slug;
        }
        return $crumbs;
    }, 10, 2);

    You may refer to the guide my colleague presented on how to apply the filter code to your website.

    Clear your website cache after doing so and let us know how that goes.

    Looking forward to helping you.

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

Viewing 5 replies - 1 through 5 (of 5 total)

The ticket ‘How to change breadcrumb’s to display URLs’ is closed to new replies.