WordPress.org

Ready to get started?Download WordPress

Forums

Customizr
Post styles stripped out (7 posts)

  1. chappie
    Member
    Posted 3 months 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
    Member
    Posted 3 months ago #

    the_excerpt() wordpress function strips html tags.
    Googlin' you can find various ways to override this behaviour, e.g.
    http://wordpress.org/support/topic/dont-strip-certain-tags-in-excerpt?replies=9
    http://aaronrussell.co.uk/legacy/improving-wordpress-the_excerpt/

  3. chappie
    Member
    Posted 3 months 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
    Member
    Posted 3 months ago #

    I told you, googlin you can find a lot of examples, this look like much more exhaustive.
    http://bacsoftwareconsulting.com/blog/index.php/wordpress-cat/how-to-preserve-html-tags-in-wordpress-excerpt-without-a-plugin/

  5. chappie
    Member
    Posted 3 months 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
    Member
    Posted 3 months ago #

    :)
    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

  7. chappie
    Member
    Posted 3 months 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…

Reply

You must log in to post.

About this Theme

About this Topic