I’ve this practice wp_trim_words()
perform set to present me excerpts of solely 20 phrases, and for essentially the most half, it really works high-quality. Aside from some purpose, on posts that include lists, it outputs far more than 20 phrases. (See the posts on the backside of this web page.) How can I repair this? I attempted eradicating <ul>,<ol>,<li>,
from the perform insight_allowedtags()
however that did nothing.
Ideally, I would prefer to make the customized excerpt output not less than 20 phrases and finish on the finish of a sentence no matter whether or not or not the submit incorporates a listing.
That is the customized perform I’m utilizing (as steered on this submit):
// Customized Excerpt for Insights Posts on Insights Web page & Class Pages
perform insight_allowedtags() {
// Add customized tags to this string
return '<script>,<model>,<span>,<ul>,<ol>,<li>,<a>,<p>';
}
if ( ! function_exists( 'insight_custom_wp_trim_excerpt' ) ) :
perform insight_custom_wp_trim_excerpt($insight_excerpt) {
world $submit;
$raw_excerpt = $insight_excerpt;
if ( '' == $insight_excerpt ) {
$insight_excerpt = get_the_content('');
$insight_excerpt = strip_shortcodes( $insight_excerpt );
$insight_excerpt = apply_filters('the_content', $insight_excerpt);
$insight_excerpt = str_replace(']]>', ']]>', $insight_excerpt);
$insight_excerpt = strip_tags($insight_excerpt, insight_allowedtags()); /*IF you'll want to permit simply sure tags. Delete if all tags are allowed */
//Set the excerpt phrase depend and solely break after sentence is full.
$excerpt_word_count = 20;
$excerpt_length = apply_filters('excerpt_length', $excerpt_word_count);
$tokens = array();
$excerptOutput = '';
$depend = 0;
// Divide the string into tokens; HTML tags, or phrases, adopted by any whitespace
preg_match_all('/(<[^>]+>|[^<>s]+)s*/u', $insight_excerpt, $tokens);
foreach ($tokens[0] as $token) {
if ($depend >= $excerpt_word_count && preg_match('/[,;?.!]s*$/uS', $token)) {
// Restrict reached, proceed till , ; ? . or ! happen on the finish
$excerptOutput .= trim($token);
break;
}
// Add phrases to finish sentence
$depend++;
// Append what's left of the token
$excerptOutput .= $token;
}
$insight_excerpt = trim(force_balance_tags($excerptOutput));
// $excerpt_end = ' <a href="'. esc_url( get_permalink() ) . '">' . sprintf(__( 'Learn extra about %s »', 'wpse' ), get_the_title()) . '</a>';
// $excerpt_more = apply_filters('excerpt_more', ' ' . $excerpt_end);
//$pos = strrpos($wpse_excerpt, '</');
//if ($pos !== false)
// Inside final HTML tag
//$wpse_excerpt = substr_replace($wpse_excerpt, $excerpt_end, $pos, 0); /* Add learn extra subsequent to final phrase */
//else
// After the content material
$insight_excerpt .= $excerpt_end; /*Add learn extra in new paragraph */
return $insight_excerpt;
}
return apply_filters('insight_custom_wp_trim_excerpt', $insight_excerpt, $raw_excerpt);
}
endif;
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'wpse_custom_wp_trim_excerpt', 'blog_custom_wp_trim_excerpt', 'event_custom_wp_trim_excerpt', 'insight_custom_wp_trim_excerpt');