Limit Archive Page Content with Read More Link in WordPress

LinkedIn
Hacker News

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 is a weblog that provides collection of programming tutorials, how-to guides and code snippets. It is the home to range of articles that aims to help developers improve their programming skills.

We have published over 500+ programming tutorials and over 300 code snippets covering various technologies.

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.