Skip to content

Instantly share code, notes, and snippets.

@robneu
Last active October 8, 2024 19:05
Show Gist options
  • Select an option

  • Save robneu/7743769 to your computer and use it in GitHub Desktop.

Select an option

Save robneu/7743769 to your computer and use it in GitHub Desktop.
Truncate the title of Yoast's WordPress SEO breadcrumbs.
<?php
/**
* Truncate the title of Yoast's WordPress SEO breadcrumbs.
*
* Sometimes you will have post titles which are longer than you want
* them to be in your breadcrumbs. This will allow you to set a maximum
* length for the post title used in Yoast's breadcrumb function.
*
* @author FAT Media, LLC
* @link http://wpbacon.com/tutorials/truncate-wordpress-seo-breadcrumbs/
*/
add_filter('wp_seo_get_bc_title', 'wpbacon_truncate_bc_title');
function wpbacon_truncate_bc_title() {
// Get the title of the current post.
$title = get_the_title();
// Determine the length of the title.
$title_length = strlen( $title );
// Set a limit for the breadcrumb title.
$limit = 55;
// Truncate the title.
$truncated = substr( $title, 0, $limit );
// Add an ellipsis if the title has been truncated.
if ( $title_length > $limit ) {
$truncated .= '...';
}
$link['text'] = $truncated;
return $link['text'];
}
@wpexplorer
Copy link

@vj009007 - Just use the core WordPress wp_trim_words() function which takes care of all that for you ;)

https://developer.wordpress.org/reference/functions/wp_trim_words/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment