0

I have a category page that I need to show some of the content of a the posts with HTML tags such as images.

Site - http://hoffwebsites.com/wp-hoff-testing/ms-conversations/

I have found this code that does the trick.

I have this in my functions.php

function technig_content($limit){
$content = explode(' ', get_the_content(), $limit);

if (count($content)>=$limit){
         array_pop($content);
         $content = implode(" ",$content).'...';
} else {
    $content = implode(" ",$content);
}
$content = preg_replace('/\[.+\]/','', $content);
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
return $content;
 }

And this where I want to the code to go.

<?php echo technig_content(170); ?>

However, it is leaving open HTML tags sometimes that throws off the page structure in some case. Usually an open bold or italic or bulleted list.

I know there is Balance Tags - https://codex.wordpress.org/Function_Reference/balanceTags

But I do not know how to incorporate that with my code.

I have tried with no luck, any thoughts?

<?php echo technig_content(170); echo balanceTags($technig_content, true); ?>
0

You better call it before returning, inside technig_content() so that it's return value is always balanced. So, the last two lines of your function would become:

$content = str_replace(']]>', ']]&gt;', $content);
$content = balanceTags( $content );
return $content;

Having said that, I don't think it's a good way of handling your output, since you don't really know how the final HTML will end up to be.

In my opinion, you should either utilize the built-in read more tag for each article, in combination with the_content(), or provide custom excerpts and use the_excerpt(). Only then you'll have total control of your end result.

  • I did not have any luck with adding that to the function I am afraid. I am importing about 700 blog posts so I can't use the read more tag for each article, it is too time consuming & I am on a deadline. – Gina DiSanto Jun 21 '17 at 17:13
  • I also need the HTML to show such as the images, client's request. – Gina DiSanto Jun 21 '17 at 17:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.