the_content for pages

Hi all,

I have made some modifications to my Wordpress installation which change the behaviour of “the_content” but I only want these changes to have an effect on single posts, not pages.

How can I make it so that it only changes “the_content” if it is a single post and not a page?

This is the code from my functions.php

function inject_ad_text_after_n_chars($content) {
  // only do this if post is longer than 1000 characters
  $enable_length = 3000;
  // insert after the first </p> after 500 characters
  $after_character = 1500;
  ob_start();
  related_posts();
  $text = ob_get_clean();
  if (is_single() && strlen($content) > $enable_length) {
    $before_content = substr($content, 0, $after_character);
    $after_content = substr($content, $after_character);
    $after_content = explode('</p>', $after_content);
    array_splice($after_content, 1, 0, $text);
    $after_content = implode('</p>', $after_content);
    return $before_content . $after_content;
  }
  else {
    return $content . $text;
  }
}
add_filter('the_content', 'inject_ad_text_after_n_chars');  

I only want this to happen on single posts, not pages. But my pages also use “the_content” so it’s coming up on them too.

Use the if (is_single()) conditional tag. Check it out here:- Conditional Tags « WordPress Codex

Hi,

I tried that and it didn’t work - it didn’t put anything on the single posts at all, they just appeared as normal and it made all of the pages blank - none of the content for them worked.

I also discovered a new problem - with the above code in place, some of my posts/pages are blank.

So I’m going to leave this for now - unless somebody else can tell me how I can get the above script to work without it making some posts blank.

Chris