PHP SEO WordPress

Modifying the length of Yoast Breadcrumbs

If you don’t know Yoast’s WordPress SEO Plugin, you’re not a real SEO. Sure, he puts a link in your Sitemap, but it’s a small price to pay for an amazing WordPress plugin.

As a reward for finishing up two posts, I decided to spend some time making some initial design changes to Bones. Most of it was pretty simple. Yet, I did have to pause to fix a minute detail that bugged me about the SEO plugin’s auto-generated breadcrumbs: if the title of a blog post exceeded a certain number of characters, the Yoast Breadcrumbs would take two lines. I decided to take a look at how I could shorten the titles in the breadcrumbs if they went over a certain number of characters.

Getting it to fix on line wasn’t as easy as it looked.

How to Modify the Length of Yoast Breadcumbs in WordPress

The first step is as simple as getting the string that is generated by the plugin as follows.

<?php if ( function_exists('yoast_breadcrumb') ) {
	$yoast = yoast_breadcrumb("","",false);

Then, you just need to create an if statement that takes into consideration the length of the current breadcrumb string. You’ll notice that the length is remarkably long for something that is usually relatively little text. That’s because the breadcrumb string also includes some HTML markup and you’ll have to truncate it all.

You might have to test different lengths depending on the width of your content div.

	if(strlen($yoast) < 585) { ?>
		<p id="breadcrumbs">
			<?php echo $yoast; ?> 
	<? } else { ?>
		<p id="breadcrumbs">
			<?php echo substr($yoast, 0, 585)."..."; ?>

As you might of guessed, the last piece of the puzzle is to close the tags that have been cut off by only using a sub-string of $yoast. If you did not select to bold the current page in the breadcrumb options, you can omit </strong>.

<?php } } ?>

You Might Also Like