WordPress - Making Yoast Breadcrumbs Behave Like A Good Boy

I mentioned in a recent post (okay, very recent) that Yoast Breadcrumbs needed some tweaking to get it to behave the way I wanted it to. This post will outline the changes that I made to my theme to get Yoast Breadcrumbs to cooperate.

First, check to see if the plugin is installed and enabled. If it is, let's get the output from the plugin:

<?php
    if(function_exists('yoast_breadcrumb'))
    {
        $breadcrumbs = yoast_breadcrumb("", "", false);
        ...
    } // end if test
?>

The first issue that I ran into is that even though I had Separator between breadcrumbs in the configuration set to &amp;raquo;, it was still throwing out » (rather than the HTML entity). To force that, I added this line:

$breadcrumbs = str_replace("»", "&raquo;", $breadcrumbs);

Since I was using a static page as my homepage and since I had all my breadcrumbs prefaced with a hyperlink to KennyCarlile.com (linked to root), I didn't want it to appear as KennyCarlile.com » About. That would just be silly. But, I wanted that root link to preface all my other pages, so I just wanted to hide the » About part.

        if(is_front_page())
        {
            echo '<a href="/">' . $breadcrumbs .
                '</a> &raquo; ' .
                '<strong>About</strong>';
        } // end if test

Now we get into the real voodoo and black magic. All other cases should be for pages where we want to display the full breadcrumb. For some reason, it was adding multiple links on occasion, depending on where it was in the site hierarchy. To handle that, I split the breadcrumb into an array, then looked at each position in the array and compared it to the next one to see if they are the same. If they are different, add it to a new array. If they are the same, ignore it and continue on.

        else
        {
            // split breadcrumb string into array
            $linksArr = split('&raquo;', $breadcrumbs);
            $newLinksArr = array(); // new links array
            $lastIndex = count($linksArr) - 1;

            // look through the links
            for($i = 0; $i <= $lastIndex; $i++)
            {
                // if 2 in a row are NOT the same...
                if(trim($linksArr[$i]) != trim($linksArr[$i + 1]))
                {
                    // ...add to the new array
                    $newLinksArr[] = $linksArr[$i];
                } // end if test
            } // end for loop

Okay, no more duplicate breadcrumb links, but now it's still linking the final breadcrumb. I want to display the page that I'm on in the breadcrumb, but it doesn't need to be a link. Why link to yourself, right? Here's some real voodoo. This is your cue to take off running if you hate regular expressions. :)

            // get the new array size
            $lastIndex = count($newLinksArr) - 1;

            // looking to extract the text from a hyperlink,
            // replace it with the same word in bold
            $pattern = '/(.*>)([^<]*)(<.*)/';
            $replace = '<strong>$2</strong>';
            $newLinksArr[$lastIndex] = preg_replace($pattern,
                                            $replace,
                                            $newLinksArr[$lastIndex]);

Cool, no more link for the page that we are on. Now we just need to turn that array back into a string and print that string to the output stream and we're done!

            // return links array to string
            $breadcrumbs = implode(" &raquo; ", $newLinksArr);    

            echo $breadcrumbs;
        } // end else test
    } // end if test
?>

That's how I solved the limitations of the Yoast Breadcrumbs plugin. I should say that I'm very impressed by what Yoast was able to do, but it just wasn't quite what I needed. I'm sure there are plenty of other ways to do this, possibly easier ones too, but this is my solution.

Here's the full code that I added to my theme to handle the breadcrumbs correctly:

<?php
    if(function_exists('yoast_breadcrumb'))
    {
        $breadcrumbs = yoast_breadcrumb("", "", false);
        $breadcrumbs = str_replace("»", "&raquo;", $breadcrumbs);

        if(is_front_page())
        {
            echo '<a href="/">' . $breadcrumbs .
                '</a> &raquo; ' .
                '<strong>About</strong>';
        } // end if test
        else
        {
            // split breadcrumb string into array
            $linksArr = split('&raquo;', $breadcrumbs);
            $newLinksArr = array(); // new links array
            $lastIndex = count($linksArr) - 1;

            // look through the links
            for($i = 0; $i <= $lastIndex; $i++)
            {
                // if 2 in a row are NOT the same...
                if(trim($linksArr[$i]) != trim($linksArr[$i + 1]))
                {
                    // ...add to the new array
                    $newLinksArr[] = $linksArr[$i];
                } // end if test
            } // end for loop

            // get the new array size
            $lastIndex = count($newLinksArr) - 1;

            // looking to extract the text from a hyperlink,
            // replace it with the same word in bold
            $pattern = '/(.*>)([^<]*)(<.*)/';
            $replace = '<strong>$2</strong>';
            $newLinksArr[$lastIndex] = preg_replace($pattern,
                                            $replace,
                                            $newLinksArr[$lastIndex]);

            // return links array to string
            $breadcrumbs = implode(" &raquo; ", $newLinksArr);    

            echo $breadcrumbs;
        } // end else test
    } // end if test
?>
Category: 

Comments

I was crushed to click on this link and find tht there was nothing about tasty bread crumbs...

You bring up a good point, John. While web breadcrumbs help you find your way back, you can't eat them if you get lost in the forest. You're just gonna have to go to the witch's house and get eaten. :)

Do you think breadcrumbs should be nofollow? I understand it is good to build PR for your higher pages but is it worth the PR that is taken from the current page?

Hmm, I suppose that's a good point to consider. My guess is that Google doesn't care much about same-domain linking that much as you have the power to abuse it easily. That said, it seems legitimate to have links to other pages within your site, but they should be contextual or related rather than just hierarchical, although I guess that is a type of relation as well. It's tough to say. I'd have to do some more research before making a good recommendation. Thanks!

Does this fix the double separator problem? I am getting two sets of >> between Home and the Category and I cannot see any reason why.

Where to add the complete code which you have posted in end?

To the index.php file?

That is a good tutorial but I still can't find anything that can help me. What I want is, to use Yoast breadcrumb as it shows on the official page.
It display links like Home » Category » Tags and all links have this html attribute.
span typeof="v:Breadcrumb" a rel="v:url" property="v:title" href="" Home a span

Sorry for entering multiple times but I wanted to show this span attribute.

Add new comment

You can optionally provide your email address, which will remain private, if you wish to be contacted directly about this comment.

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Refresh Type the characters you see in this picture. Type the characters you see in the picture; if you can't read them, submit the form and a new image will be generated. Not case sensitive.  Switch to audio verification.