Skip to content

Instantly share code, notes, and snippets.

@wpflames
Last active February 11, 2022 15:21
Show Gist options
  • Select an option

  • Save wpflames/0a7c3e5bdad3c9d3fada1a19ae246827 to your computer and use it in GitHub Desktop.

Select an option

Save wpflames/0a7c3e5bdad3c9d3fada1a19ae246827 to your computer and use it in GitHub Desktop.
Shorten the post title in WordPress
<?php // Don't copy the opening tag
// =========================================================================
// SHORTEN THE TITLE
// =========================================================================
function shortenTitle(){
$thetitle = get_the_title();
$getlength = strlen($thetitle);
$thelength = 45;
echo substr($thetitle, 0, $thelength);
if ($getlength > $thelength) {
echo "<span class='readmore'>...</span>";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment