Rank Math Breadcrumbs:
“Home” on Home Only

What are Breadcrumbs and Why Have Them?

First, an example for this very page:

You are here: Blog » WordPress » Rank Math Breadcrumbs: “Home” on Home Only

In case you don’t know already, breadcrumbs show the name of the current website page and a hierarchical path leading to it. They visually represent how to get here again. The term comes from the trail of bread crumbs left by Hansel and Gretel in the popular fairy tale. As in the fairy tale, breadcrumbs help keep people from getting lost.

The pages up the hierarchy are typically links, making it easy to move to a page that is one or more levels higher with a single click. This can minimize the number of clicks one must make to navigate around the site.

Since breadcrumbs don’t take up much screen space–typically just a single line–they are an excellent way to improve the user’s grasp of the navigation structure without clutter. While breadcrumbs may seem like a small detail, their impact on user experience and navigation efficiency is significant. They help to keep your users happy and engaged with your site.

Breadcrumbs can also improve search engine optimization (SEO) by aiding search engines to discern the structure of a website. Furthermore, if you have important key words in your page titles, those keywords will be repeated again in the breadcrumbs. Unless you are stuffing (i.e., seriously over-using) keywords, this, too, will help your SEO ranking.

Popular WordPress SEO plugins, including Yoast SEO and Rank Math, include an option to turn on breadcrumbs because they recognize their value to SEO.

The Problem with the “Home” Breadcrumbs Setting in Rank Math

Surely I’m not the only one who thinks that having every breadcrumb start with “Home” is silly, especially if you have a link to the home page nearby already. Besides, showing “Home” before all other pages implies that the home page is the hierarchical “parent” of all those pages. Well, it is not!

The people behind Rank Math must recognize that some of us feel this way, as the plugin has a setting to choose between always showing “Home” as part of the breadcrumbs or never showing it. However, leaving “Home” out creates another problem, which I will get to in a moment.

For example, the settings let you choose between something like:

You are here: Home » Account » Work Request

and

You are here: Account » Work Request

There are so many ways to get back to the home page that I really think a “Home” link in all of the breadcrumbs is superfluous (at least on my website), so I want my site to look like the second example, above.

Here is the problem: With “Home” turned off, when you are on the home page, you will see:

You are here:

instead of:

You are here: Home

I don’t know about you, but if I see, “You are here:” then I expect something to follow the colon. The fact that nothing does makes me think there is a bug in the web page.

Solve the “Home” Page Problem with PHP Code

Neither showing nor hiding the “Home” page from the breadcrumbs is simultaneously acceptable for both the “Home” page and all the others, so I looked for a solution. As I found only discussions about other breadcrumbs issues, I thought I would save you the trouble of having to reinvent my answer.

I used WordPress PHP code to solve the problem. Lest that worry you, I will share my code and tell you where to put it.

First, you just need these six lines of code:

add_filter( 'rank_math/frontend/breadcrumb/items', function( $crumbs, $class ) {
    if (count($crumbs) > 1) {
        array_splice($crumbs, 0, 1);
    }
    return $crumbs;
}, 10, 2);

Here is what the code does:

  1. The “add_filter” line intercepts a Rank Math function and grabs two variables that result from it. We only care about the first one, $crumbs.
  2. In the line starting with “if,” the count() function counts the number of entries in the $crumbs variable. $crumbs is, in fact, an array, which is a special variable that holds a collection of entries. We are counting the number of entries in its collection. If the number of entries is exactly 1, then in this particular case, only the data for the home page is inside $crumbs. Because I have the “Home” setting turned on in Rank math, then for every page except the “Home” page, the count of entries in $crumbs will be 2 or more. So the program is saying, if the count of entries in crumbs is greater than 1, do the next thing, which will change our breadcrumbs.
  3. When that aforementioned count is greater than 1, it uses the PHP “array_splice()” function to remove the first entry, which is for “Home,” while leaving everything else there. (The “0” tells the array_splice function to remove the initial entry, and the “1” tells it to stop removing after removing one single entry. The array_splice function can also combine two arrays together, but we aren’t doing that here.)
  4. Regardless of whether or not the “Home” entry was removed, return the $crumb variable to the function that called ours in the first place, so that the result of what we did–or did not do–will be used to display the breadcrumbs.

I also added a note above my code, to warn about the related setting in Rank Math. In PHP, notes or comments are preceded by double slashes (//); without the slashes your comments will be interpreted as code, and since they are not, the program will crash, taking your site with it. Anyway, here is my code with my note:

// NOTE: 
// For the code below to work, Rank Math should be set to have "Home" 
// showing in the breadcrumbs. Otherwise you will be removing things 
// you don't want to.
add_filter( 'rank_math/frontend/breadcrumb/items', function( $crumbs, $class ) {
    if (count($crumbs) > 1) {
        array_splice($crumbs, 0, 1);
    }
    return $crumbs;
}, 10, 2);

I will tell you where to put the code in a moment. Before you do that, back up your website. (Better yet, clone your website to a test site and try the following steps on your test site first.)

Next, make sure that your settings are as I already mentioned, as follows:

  1. In the WordPress dashboard, go to Rank Math SEO -> General Settings.
  2. Click the Breadcrumbs tab.
  3. If “Enable breadcrumbs function” is turned off, then turn it on.
  4. Turn on “Show Homepage Link.”

Read through the other Rank Math options and change any you like.

You can put the PHP code above in your child theme’s functions.php file or create a new plugin to hold it. For anyone who has never done either, I recommend the first approach, which you can learn more about in “How to Create a WordPress Child Theme (Beginner’s Guide)” at wpbeginner.

Conclusion

One of the virtues of WordPress is its vast potential for finely-tuned enhancements. Not only can you extend the capabilities of WordPress itself, but in many cases, you can also alter and enhance the plugins that themselves build upon the WordPress core.

In this article, I showed you one way that you do not have to settle for just what a plugin offers. You can change the plugin’s output to suit your particular needs. I hope I have helped empower you to make the Web more sensible, beautiful, fun, and better for your users!


Need help making your website match your vision? Contact me!

Leave a Comment

TOC