Hi @kendrajk,
Thanks for your message.
Does it work if you manually include an excerpt for the post as opposed to an automatic one?
Thread Starter
KKSL
(@kendrajk)
Yes it does work if I put in a manual excerpt. Is there anyway around that so it can grab the automatic excerpt instead?
Hi @kendrajk,
As far as I know it should work.
Can you share the code for your excerpt?
Thread Starter
KKSL
(@kendrajk)
Here’s what I’m using for the excerpt.
function wpse_allowedtags() {
// Add custom tags to this string
return '<script>,<style>,<br>,<em>,<i>,<ul>,<ol>,<li>,<a>,<p>,<strong>,<em>';
}
if ( ! function_exists( 'wpse_custom_wp_trim_excerpt' ) ) :
function wpse_custom_wp_trim_excerpt($wpse_excerpt) {
global $post;
$raw_excerpt = $wpse_excerpt;
if ( '' == $wpse_excerpt ) {
$wpse_excerpt = get_the_content('');
$wpse_excerpt = strip_shortcodes( $wpse_excerpt );
$wpse_excerpt = apply_filters('the_content', $wpse_excerpt);
$wpse_excerpt = str_replace(']]>', ']]>', $wpse_excerpt);
$wpse_excerpt = strip_tags($wpse_excerpt, wpse_allowedtags()); /*IF you need to allow just certain tags. Delete if all tags are allowed */
//Set the excerpt word count and only break after sentence is complete.
$excerpt_word_count = 45;
$excerpt_length = apply_filters('excerpt_length', $excerpt_word_count);
$tokens = array();
$excerptOutput = '';
$count = 0;
// Divide the string into tokens; HTML tags, or words, followed by any whitespace
preg_match_all('/(<[^>]+>|[^<>\s]+)\s*/u', $wpse_excerpt, $tokens);
foreach ($tokens[0] as $token) {
if ($count >= $excerpt_word_count && preg_match('/[\,\;\?\.\!]\s*$/uS', $token)) {
// Limit reached, continue until , ; ? . or ! occur at the end
$excerptOutput .= trim($token);
break;
}
// Add words to complete sentence
$count++;
// Append what's left of the token
$excerptOutput .= $token;
}
$wpse_excerpt = trim(force_balance_tags($excerptOutput));
return $wpse_excerpt;
}
return apply_filters('wpse_custom_wp_trim_excerpt', $wpse_excerpt, $raw_excerpt);
}
endif;
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'wpse_custom_wp_trim_excerpt');
Thanks @kendrajk,
Does it work if you don’t use your custom code, i.e. comment out the remove_filter and add_filter lines?
Thread Starter
KKSL
(@kendrajk)
Yes it does work if I take the function out. Is there any way to make it work WITH the function? Thanks.
Hi @kendrajk,
You may wish to investigate your function as something in your code is causing it to not show.
I would start by making it as simple as possible at first, then adding complexity until you find the issue.
Let me know how you get on.
Closing due to inactivity. If you need further help with this, please feel free to re-open this thread.