I have a question after reading this post (How to highlight search terms without plugin). I like this function(Search Term Without Plugin) very much but the character length is too long. What php code should i add to make the excerpt shorter? Would appreciate if someone can suggest it. Thank you!
Leave a Reply
You must be logged in to post a comment.
add these lines in function.php file
In addition to the above filter hook supplied by Deepa’s answer here is one additional function that can help you extend the use of
the_excerpt
in two ways,Allows you to…
Limit the excerpt by number of characters but do NOT truncate the last word. This will allow you to return a maximum number of characters but preserve full words, so only the words that can fit within the specified number limit are returned and allow you to specify the source of where the excerpt will come from.
This function can be used multiple times through out theme files, each with different character limits specified.
This function has the ability to retrieve an excerpt from either,
the_content
the_excerpt
For example, if you have posts that contain text in the_excerpt box on the post editor screen, but want to pull an excerpt from the_content body instead for a special use case you would instead do;
This tells the function that you want the first 140 characters from
the_content
, regardless of whether an excerpt is set inthe_excerpt
box.This tells the function that you want the first 140 characters from
the_excerpt
first and if no excerpt exists,the_content
will be used as a fallback.The function can be improved to be made more efficient and or incorporated with the use of WordPress filters for both
the_content
orthe_excerpt
or simply used as is in situations where there is no suitable, in-built WordPress API alternative.