I'm displaying a preview of a post/page/cpt using a 'work-around' that i found. This 'work-around' gets the post content, trim it and render the trimmed content with it's html elements ( links, images etc.. ), so similar to the_content()
.
The mentioned work-around is this
<?php
$more = '...';
echo force_balance_tags( html_entity_decode( wp_trim_words( htmlentities( $values["content"] ), 300, $more ) ) );
echo '<a href="'. $link . '" class="readMoreBtn">Read More</a>'
?>
This little code
- displays preview of the post correctly if the content doesn't get trimmed when a comment starts
- breaks the html when it ends with a non-closed started comment
<!--
i mean sometimes happens that the trimmed page content ends like this
<!-- wp:image...<a href="/" class="readMoreBtn">Read More</a>
so i suppose that the image that was going to be displayed started but the comment has never been closed. This cause that the whole html code under that output get commented unitl it encounters a close comment tag -->
.
As a solution for this i changed
Before: echo '<a href="'. $link . '" class="readMoreBtn">Read More</a>'
After: echo '<!-- --><a href="'. $link . '" class="readMoreBtn">Read More</a>'
So that the close comment tag is find instantly.
I supposed that the force_balance_tags
could balance all the tags in the string, am i wrong?
There is a better way to solve this rather than adding <!-- -->
right after the built preview's echo
?