Best way to limit WordPress title length without plugin
The best way to limit the WordPress title length without a plugin is to add code in to your themes functions.php file and call upon that function from within your theme template.
Here’s how to do it:
- Open functions.php in your theme
- Paste the following code in to functions.php:
-
function max_title_length( $title ) {$max = 10;if( strlen( $title ) > $max ) {return substr( $title, 0, $max ). ” …”;} else {return $title;}}
- Note – you can change the value of 10, to the amount of characters you wish to limit by (including spaces).
-
- Paste this line of php code in to the file containing titles that you wish to limit, e.g. archive.php:
- add_filter( ‘the_title’, ‘max_title_length’);
Example Use:
You may wish to limit the size of titles if you are using a grid layout for your post archive, or main blog index.
This website (wploop), explains in more detail where you can apply this function.