SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Oct 2010
    Posts
    167
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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


    Code 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.

  2. #2
    SitePoint Addict Poiesis01's Avatar
    Join Date
    Jun 2007
    Location
    Cape Town
    Posts
    233
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use the if (is_single()) conditional tag. Check it out here:- Conditional Tags WordPress Codex

  3. #3
    SitePoint Zealot
    Join Date
    Oct 2010
    Posts
    167
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •