1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to Add add code middle of the wordpress post content

Discussion in 'WordPress' started by soney218, Feb 17, 2011.

  1. #1
    Here i would like share how to put Adsense code or any other ads code inside the WordPress post content

    1. Go to Appearance –> Editor and select functions.php
    2. Copy and past the following code in functions.php and save it
    function inject_ad_text_after_n_chars($content) {
      // only do this if post is longer than 1000 characters
      $enable_length = 1000;
      // insert after the first </p> after 500 characters
      $after_character = 500;
      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);
        $text = '
          <!-- PUT YOUR AD HERE -->
        ';
        array_splice($after_content, 1, 0, $text);
        $after_content = implode('</p>', $after_content);
        return $before_content . $after_content;
      }
      else {
        return $content;
      }
    }
    add_filter('the_content', 'inject_ad_text_after_n_chars');
    PHP:
    3.After adding the above code replace <!– PUT YOUR AD HERE –> with your adsense or any advertisement code.
     
    soney218, Feb 17, 2011 IP
    Ajeet and Digital_shubhi like this.
  2. projectx

    projectx Active Member

    Messages:
    884
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    68
    #2
    Thank you , i was looking for the code, without the use of plugin.
     
    projectx, Feb 19, 2011 IP
  3. TechnoBombs

    TechnoBombs Member

    Messages:
    219
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #3
    Great tip !

    What if I want to show code on my home page, where I can't use more than three ad blocks, since google doesn't allow more than 3 link ad blocks !
     
    TechnoBombs, Feb 19, 2011 IP
  4. Dodger

    Dodger Peon

    Messages:
    1,494
    Likes Received:
    60
    Best Answers:
    0
    Trophy Points:
    0
    #4
    That code only displays the ad block on Single pages. Your home will not show them. No worries.
     
    Dodger, Feb 19, 2011 IP
  5. projectx

    projectx Active Member

    Messages:
    884
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    68
    #5
    The code show only on the single pages, how can i get them on the index.php ?
     
    projectx, Feb 19, 2011 IP
  6. Dodger

    Dodger Peon

    Messages:
    1,494
    Likes Received:
    60
    Best Answers:
    0
    Trophy Points:
    0
    #6
    As the other poster pointed out, this may not be a good idea for AdSense. Terms may not allow it (I don't know if they do or not). If you are bent on placing ads on every post on the home page, then change the conditional if statement where it checks for is_single to:

      if ( [B](is_single() || is_home())[/B] && strlen($content) > $enable_length) {
    Code (markup):
    Note that I included an OR conditional within containing parenthesis. This check is done before applying the AND on the end.
     
    Dodger, Feb 19, 2011 IP
  7. projectx

    projectx Active Member

    Messages:
    884
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    68
    #7
    I have only 1 ad on the index php so it should be a problem, but when i insert the code you give me it won's show up on theCategories, can you help me with that?
     
    projectx, Feb 19, 2011 IP
  8. Dodger

    Dodger Peon

    Messages:
    1,494
    Likes Received:
    60
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Add more conditionals to the OR clause of that line. You have things like is_category(), is_page(), etc. Not that hard to figure out. Search for conditional tags at the WordPress Codex.
     
    Dodger, Feb 19, 2011 IP
  9. projectx

    projectx Active Member

    Messages:
    884
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    68
    #9
    It works , awesome thank you Dodger!
     
    projectx, Feb 19, 2011 IP
  10. Dodger

    Dodger Peon

    Messages:
    1,494
    Likes Received:
    60
    Best Answers:
    0
    Trophy Points:
    0
    #10
    No problem. Rep me! :)

    Would want to add, that will only work if you are displaying the entire post on those pages. If you are using "truncated" the_content() or the_excerpt() inside of those loops, then the ad will not show. You can show ads by keeping count of the items that are displayed, and showing ads every 3rd item (for example).
     
    Dodger, Feb 19, 2011 IP