Skip to content

Instantly share code, notes, and snippets.

@navnit-viradiya
Created August 30, 2018 11:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save navnit-viradiya/f1f247bedc346ca2d260160c7efaf00d to your computer and use it in GitHub Desktop.
Save navnit-viradiya/f1f247bedc346ca2d260160c7efaf00d to your computer and use it in GitHub Desktop.
Set character limit on get_the_content() or get_the_excerpt() in wordpress
<?php
function break_content($text,$length){
$text = trim($text);
if(strlen($text)<$length+10)
return $text;//don't cut if too short
$break_pos = strpos($text, ' ', $length);//find next space after desired length
$visible = substr($text, 0, $break_pos);
if(is_front_page()){
return balanceTags($visible) .'<a class="aerrow-link" href="' . get_permalink( get_page_by_path( 'testimonials' ) ).'#'. basename(get_permalink()). '"> ...Read More</a>';
}
}
echo break_content(get_the_content(),150);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment