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/
Hi It was very helpful code. Thanks .
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.
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.
You need click on buttons where code is hidden. Than only, you are able to see the code. If you still facing issue than write email me. purabdk@gmail.com
This method also works very good:
<?php
echo substr(get_the_excerpt(), 0,100)."…";
echo'ID) . ‘”>lees meer‘;
?>
good one:
then after that make a link with the read more tag: