• Daffydd57

    (@daffydd57)


    Hello,

    Bit of a problem. I’m using get_the_content and mb_trimwidth to control the number of characters in posts. Since the posts (created by plugin that generates posts from RSS feeds) contain html sometimes the truncation will leave an open html tag – for example if there is opening strong tag and it truncates before that tag is closed then all subsequent text will be bold.

    Is there a way to auto close all open tags?

    Here is the code I’m using to display the posts on my webpage:

    <?php $content = get_the_content(); echo mb_strimwidth($content, 0, 850, '...');?>

    This is part of the loop as the posts are being displayed outside the theme.

    Thank you for any suggestions!
    Daf

Viewing 5 replies - 1 through 5 (of 5 total)
  • Matt Knowles

    (@aestheticdesign)

    Rather than try close the tag, I would try adding a second trim to truncate the string at the last < found.

    Thread Starter Daffydd57

    (@daffydd57)

    Thanks, Matt – that sounds like a plan! I’ll see what I can find out about that. Any leads as to what terms may yield the best search results? Is there a php or wordpress function already in place for that?

    Thanks again!

    Thread Starter Daffydd57

    (@daffydd57)

    Not really finding anything like that … could use a shove in the right direction, please. ?? How would one truncate the_content without breaking a html tag or middle of a word?

    Any suggestions would be appreciated!

    Thanks!

    Matt Knowles

    (@aestheticdesign)

    It would be something like this:

    <?php
    $content = get_the_content();
    $string = mb_strimwidth($content, 0, 850, '...');

    Then find the last occurrence of < in the string and use that position to trim the string again. I’m not familiar with multibyte functions so I can’t provide you exact code. for this step. Then after you’ve trimmed the string a second time:

    echo $string;
    
    ?>
    Thread Starter Daffydd57

    (@daffydd57)

    Thanks Matt – I was able to find a php class that worked like I needed it to.

    Daf

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Unclosed html tags (get_the_content)’ is closed to new replies.