StackTips

Limit Archive Page Content with Read More Link in WordPress

stacktips avtar

Written by

Editorial,  2 min read,  2.84K views, updated on Sept. 17, 2023

Most of the classic WordPress blog themes display full content in archive page. If you want to limit the archive post content with a read more button, then add the following snippet to your theme function.php file.

Example:
Limit Archive Page Content and Add Read More Link in WordPress

add_filter("the_content", "break_text");
function break_text($text){
  if(is_front_page() || is_archive() || is_search())
  {
    $length = 400; // limited to 400 characters
    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);
	$read_more = "... <br><center><a href='".get_permalink()."' class='wp-btn'>Read more..</a></center>";
    return balanceTags($visible) . $read_more;
  } else {
    return $text;
  }
}