Limit excerpt length by characters in wordpress

Limit excerpt length by characters in wordpress

On wordpress home page many sites are showing the post description or we can say the excerpt. For UI purpose some time we need to control the excerpt character limit.  I already written about this in following article. But some people need the more advanced excerpt. Following code snippet will be helpful to you show the limit excerpt length by characters in wordpress.

First open your functions.php file and put following code in that file.

[viral-lock message=”Solution code is Hidden! It’s Visible for Users who Liked/Shared This article on Facebook or Twitter or Google+. Like or Tweet this article to reveal the content.”]

function get_excerpt($count){
  $permalink = get_permalink($post->ID);
  $excerpt = get_the_content();
  $excerpt = strip_tags($excerpt);
  $excerpt = substr($excerpt, 0, $count);
  $excerpt = substr($excerpt, 0, strripos($excerpt, " "));
  $excerpt = $excerpt.'... <a href="'.$permalink.'">more</a>';
  return $excerpt;
}

[/viral-lock]

Use this function if you’re planning on using it more than once with a different amount of characters.

Call the function plus the amount of characters –

<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php echo get_excerpt(125); ?>

Following is my old article link.
https://purabtech.in/set-wordpress-post-excerpt-length-limited-characters/

Limit excerpt length by characters in wordpress
Limit excerpt length by characters in wordpress

Published by

Purab

I am Purab from India, Software development is my profession and teaching is my passion. Programmers blog dedicated to the JAVA, Python, PHP, DevOps and Opensource Frameworks. Purab's Github Repo Youtube Chanel Video Tutorials Connect to on LinkedIn

6 thoughts on “Limit excerpt length by characters in wordpress”

  1. This code works brilliantly but, is there a way to preserve a space between p tags? If the excerpt is automatically generated from the post and there is a line break, what you wind up with is no space between the end of one sentence and the start of another. How do you strip_tags but add a space where p’s used to be?

    Something like this.Does not look right.

  2. I shared this on Twitter and G+ and I can’t see the code.
    To be honest, it’s disappointing to have to share something I don’t know even works!

    I’m sure I can find this somewhere else.

    Thanks.

Leave a Reply

Your email address will not be published.