Adding Adsense to Wordpress Comments

Discussion in 'PHP' started by bschneider5, Jun 27, 2009.

  1. #1
    I'm sure it can be done, not sure how to do it. I'd like to display and adsense ad block after the tenth comment on my Wordpress blog. Anyone know what code I need to add? Any help would be tremendously appreciated.
     
    bschneider5, Jun 27, 2009 IP
  2. wd_2k6

    wd_2k6 Peon

    Messages:
    1,740
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Yep it can be done, try something like this:

    Inside comments.php or whatever comments template you use:

    anywhere below the line

    <?php if ($comments) : ?>
    PHP:
    add something like:
    
    <?php if (comment_ID() == 10) {
    echo '//ADSENSE CODE HERE'; 
    ?>
    
    PHP:
    Wherever you want the ad block to appear.
     
    wd_2k6, Jun 28, 2009 IP
  3. bschneider5

    bschneider5 Active Member

    Messages:
    1,009
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    88
    #3
    Seems no matter where I place it in the comments.php it never displays??!?!?!?!
     
    bschneider5, Jun 28, 2009 IP
  4. wd_2k6

    wd_2k6 Peon

    Messages:
    1,740
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I've had a quick look and I don't think they count the comments so we can do it ourselves.

    OK firstly to check that your changes are working:

    Anywhere below the line:

    <?php foreach ($comments as $comment) : ?>
    PHP:
    Just add the text

    <h1>Testing Text</h1>
    Code (markup):
    And re-upload the file and reload your page and check that this testing text appears inside every comment.

    OK if the test was successfull, now replace <h1>Testing Text</h1> with:
    
    <?php
    $counter ++; 
    if ($counter == 3) { 
    echo 'ADSENSE CODE GOES HERE';
    }
    ?>
    
    PHP:
    I tested this and it's working so it should work. Change the number 3 to the number of the comment you'd like it to appear in.
     
    wd_2k6, Jun 28, 2009 IP