Here is my excerpt code.
// Generate custom excerpt length
function wpbx_excerpt_length($length) {
return 300;
}
add_filter('excerpt_length', 'wpbx_excerpt_length');
How do I allow html like <a> <b> <i> <br>
Here is my excerpt code.
// Generate custom excerpt length
function wpbx_excerpt_length($length) {
return 300;
}
add_filter('excerpt_length', 'wpbx_excerpt_length');
How do I allow html like <a> <b> <i> <br>
You must be logged in to post a comment.
COMPLETE GUIDE TO EXCERPTS
I’ve recently answered a few questions regarding excerpts, so I’m going to give a detailed explanation covering as much as I can.
PREFACE
There seems to be a couple of questions arising from this answer on where the code should go, and the answer is, it is really up to you and how you see fit. There are are a couple of options where you can place the code (if not explicitly stated):
In your theme’s functions.php or any file use as a functions file. Just remember when you do this, if the theme is not your own, all changes will be lost when you upgrade your theme
A better way would be to use the code in a child theme. As above, in the functions.php or functions related file
Use the code in a plugin. This is the prefered way as this makes the code available across all themes. If you switch themes, you don’t have to worry about rewriting the same code.
I hope this clears things up a bit 🙂
HTML TAGS/FORMATTING
the_excerpt()
first of all doesn’t accept any parameters, so nothing can be passed to it. It is a fact thatthe_excerpt()
trims the content to 55 words, and all HTML tags are stripped before returning the text.the_excerpt()
is located in wp-includes/post-template.php. To allow certain or all HTML tags in the excerpt, a new excerpt has to be created.First of all, the original function needs to be removed first, and then the new function needs to be hooked to
get_the_excerpt
. Please take note, this new excerpt will still be callable asthe_excerpt()
in template files, no need to change that.get_the_excerpt()
is located in wp-includes/post-template.php.The excerpt uses
wp_trim_excerpt
to return the trimmed text, so we need to removewp_trim_excerpt
first from the excerpt filter.wp_trim_excerpt()
is located in wp-includes/formatting.php, line 2355. This is how:You can now add your new excerpt to
get_the_excerpt
To allow html tags/formatting, we will need to specify which tags you will need to allow. You can use the following
strip_tags
statement to achieve thatThe second argument
wpse_allowedtags()
is a small function that is used to add the tagsthe_excerpt()
will allow. For a complete list of valid HTML 5 tags, go and check it out here. Here is function, add any html tags to this that you need to allow/keepIf you need to allow all HTML tags, that is, no stripping of any tags, the
strips_tags()
function can be omitted/removed completely.A point to note however, when html tags are allowed, these tags are counted as words, so your word count for excerpts with tags and without tags will not be the same. To correct that, you will need to remove these tags from the actual word count first so that only words are counted.
I have written an excerpt that will allow all tags, count only words as words, and complete a sentence after the set amount of words (so the text won’t be trimmed mid-sentence) and add a read more text after the last word.
Here is the complete code
You can just remove the ‘//’ from functions that you need extra.
CUSTOM EXCERPT LENGTHS
Sometimes you need to display simple excerpts of different lengths and it is not viable to write an excerpt for every post/function/page. Here is a nice small little function using
wp_trim_words
What this little function does is taking
get_the_excerpt
, trimming it to$limit
set by the user, and returning the text with a read more link at the end.You can call this excerpt as follow in your template
where
$limit
will be your word count, so an excerpt of 30 words will beJust one thing to remember here, if you set your limit to more that 55 words, only 55 words will be returned as the excerpt is only 55 words in length. If longer excerpts are needed, use
get_the_content
instead.CUSTOM EXCERPT LENGTH
If you just need to alter the length of
the_excerpt()
, you can use the following functionRemember, you will need to set a priority bigger than 10 so that your custom function executes after the default.
ADD READ MORE LINK
All text returned by the excerpt have the hated
[...]
at the end that is not clickable. To add a read more text in the place of the hellips, use this functionEDIT
Excerpt first paragraph
I want to keep this complete, so here is the excerpt that trims after the first paragraph.
Here is a function that keeps HTML tags in tact, adds a “Read More” link at the end of the excerpt and trims the excerpt after the first paragraph.
EDIT 29-10-2015
For anyone that need a workaround to not display the read more link after the excerpt when the excerpt is shorter that the amount of words set, please see the following question and answer
Add more tags if you need into
$allowed_tags = ...
From :http://bacsoftwareconsulting.com/blog/index.php/wordpress-cat/how-to-preserve-html-tags-in-wordpress-excerpt-without-a-plugin/
You can add rich text editor for excerpts as well, add below code in plugin file or theme’s function.php file and you’ll be able to see HTML editor for excerpts. Moreover, it’ll render excerpts in HTML format as well. #cheers
I’ve copied this from somewhere but don’t remember the source. I’m using this in my all projects and it’s working.
Edit: This was copied from Adding a rich text editor to Excerpt 2012 answer by fuxia