Post styles stripped out (7 posts)

  1. chappie
    Posted 2 years ago #

    I'm trying to work out how to preserve text styling applied to a standard post when the extract of that post is displayed on my front blog post summary page (in an offline localhost setup).

    The opening few lines include some italic styling, a hyperlink and a paragraph break - all of which are stripped out in the automatically created blog page summary…as can be seen in this screen grab comparing the full post (top) with the extract as it appears on the front blog page (bottom).

    How can I fix this please?

  2. d4z_c0nf
    Posted 2 years ago #

    the_excerpt() wordpress function strips html tags.
    Googlin' you can find various ways to override this behaviour, e.g.

  3. chappie
    Posted 2 years ago #

    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]

  4. d4z_c0nf
    Posted 2 years ago #

    I told you, googlin you can find a lot of examples, this look like much more exhaustive.

  5. chappie
    Posted 2 years ago #

    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.

  6. d4z_c0nf
    Posted 2 years ago #

    Well you can always choose to display the full content in post lists, and then add the tag more to your articles.

    don't know if you looked at this comment that should solve the problem with unbalanced tags.


  7. chappie
    Posted 2 years ago #

    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…

Topic Closed

This topic has been closed to new replies.

About this Theme

About this Topic