-
-
Save robneu/7743769 to your computer and use it in GitHub Desktop.
You can also check out my plugin here: https://github.com/wpexplorer/wpex-yoast-breadcrumbs-trim-title
The tutorial is not working anymore. The link is broken. Any advice?
@naturetti
// truncate Yoast SEO breadcrumb title
add_filter('wp_seo_get_bc_title', function($link_text, $id) {
if ( strlen( $link_text ) <= ($limit = 20) ) return $link_text;
return substr( $link_text, 0, $limit ).'…';
}, 10, 2);
A new warning shows up:
Notice: wp_seo_get_bc_title is deprecated since version WPSEO 5.8! Use wpseo_breadcrumb_single_link_info instead. in /home/username/public_html/wp-includes/functions.php on line 4089
is there anyone who can help? Thank you :)
using the new filter will look something like this (I have changed it slightly from the example above)
function shorten_yoast_breadcrumb_title($link_info)
{
$limit = 20;
if (strlen($link_info['text']) > ($limit)) {
$link_info['text'] = substr($link_info['text'], 0, $limit) . '…';
}
return $link_info;
}
add_filter('wpseo_breadcrumb_single_link_info', 'shorten_yoast_breadcrumb_title', 10);
Hi,
can it also be done with yoast meta title?
Hi,
can it also be done with yoast meta title?
@lealustore There's nothing impossible when it comes to coding... ๐๐๐
Here's the function to truncate Yoast Meta Title:
function limit_title_yoast( $str ) {
return substr($str, 0, 65); //Replace number 65 with your desired character
}
add_filter( 'wpseo_title', 'limit_title_yoast' );
Hi,
can it also be done with yoast meta title?@Lealustore There's nothing impossible when it comes to coding... ๐๐๐
Here's the function to truncate Yoast Meta Title:
function limit_title_yoast( $str ) { return substr($str, 0, 65); //Replace number 65 with your desired character } add_filter( 'wpseo_title', 'limit_title_yoast' );
@robinurislamsohan
good, if i want to limit only post title variable on meta title? can it also be done ?
@robinurislamsohan
would it be possible to limit only the yoast variable title?
Improved @keefyhub code to avoid cutting UTF8 chars in half (and avoid breadcrumbs like "sin๏ฟฝโฆ").
function shorten_yoast_breadcrumb_title($link_info)
{
$limit = 32;
if (strlen($link_info['text']) > ($limit)) {
$link_info['text'] = substr(
$link_info['text'],
0,
strpos($link_info['text'], ' ', $limit) // added: cut at the first white space after limit
) . '…'; }
return $link_info;
}
add_filter('wpseo_breadcrumb_single_link_info', 'shorten_yoast_breadcrumb_title', 10);
Hi,
can it also be done with yoast meta title?@lealustore There's nothing impossible when it comes to coding... ๐๐๐
Here's the function to truncate Yoast Meta Title:
function limit_title_yoast( $str ) { return substr($str, 0, 65); //Replace number 65 with your desired character } add_filter( 'wpseo_title', 'limit_title_yoast' );
This doesn't work now.
Improved @keefyhub code to avoid cutting UTF8 chars in half (and avoid breadcrumbs like "sin๏ฟฝโฆ").
function shorten_yoast_breadcrumb_title($link_info) { $limit = 32; if (strlen($link_info['text']) > ($limit)) { $link_info['text'] = substr( $link_info['text'], 0, strpos($link_info['text'], ' ', $limit) // added: cut at the first white space after limit ) . '…'; } return $link_info; } add_filter('wpseo_breadcrumb_single_link_info', 'shorten_yoast_breadcrumb_title', 10);
Its working on the latest version of Yoast
@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/
You can find a full explanation of this code and how to use it here: http://wpbacon.com/tutorials/truncate-wordpress-seo-breadcrumbs/