Thank you, @d4z_c0nf – your second link in particular helped me but I was a bit shocked to discover that WP thinks this is good default behaviour. I’m trying to think of a “benefit” in stripping out text style tags and hyperlinks but I’m coming up empty — unless it was just too difficult to auto-close tags if the except happened to slice a style in half…
But judging by your second link, and the comments thereto, it’s achievable. Unfortunately php stuff is some way above my pay grade unless I grab a Customizr snippet. I think I like the look of the code in the final comment – but I don’t know exactly what it will do:
[code] function my_trim_excerpt($text) { global $post; if ( ‘’ == $text ) { $text = get_the_content(‘’); $text = apply_filters(‘the_content’, $text); $text = str_replace(‘]]>’, ‘]]>’, $text); $text = preg_replace(‘@]?>.?@si’, ‘’, $text); $text = strip_tags($text, ‘’); $excerpt_length = 30; $words = explode(‘ ‘, $text, $excerpt_length + 1); if (count($words)> $excerpt_length) { array_pop($words); $atag_count = 0; foreach($words as $word) { if(strpos($word, ‘<a') !== false || strpos($word, '') !== false || strpos($word, '/A>')) { $atag_count--; } } for($i=0; $i<$atag_count; $i++) { array_push($words, '</a>'); } array_push($words, '...'); $text = implode(' ', $words); } } return $text; } [/code]
Thanks! That’s an even better link. I might risk playing with the code in my child theme — after a stiff drink or two.
Still not sure why WP does it this way. The default behaviour is just ugly imho.
🙂
Well you can always choose to display the full content in post lists, and then add the tag more to your articles.
p.s.
don’t know if you looked at this comment that should solve the problem with unbalanced tags.
http://bacsoftwareconsulting.com/blog/index.php/wordpress-cat/how-to-preserve-html-tags-in-wordpress-excerpt-without-a-plugin/comment-page-4/#comment-10698
Thanks – I did see that code and will try it out. I just hope it doesn’t need any modification as I can’t read/understand php so not sure what this snippet will give me…