Limit Archive Page Content with Read More Link in WordPress

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;
  }
}
Tagged in : #PHP #WordPress

Stacktips

StackTips provides programming tutorials, how-to guides and code snippets on different programming languages. Follow us on Twitter

Join The Discussion

Please note: We reserve the right to delete comments that contains snarky remarks, offensive or off-topic. To know more read our comments policy.