• davem.org
  • random
  • December 5, 2006

    WordPress K2: Google AdSense Under First Post Only

    Filed under: — davemorg @ 9:20 pm

    I was scanning the Web for information on how to add a Google AdSense block under only the first post on a Wordpress installation. While I found a good deal of information, I couldn’t find very much of anything that applied specifically to the popular K2 theme.

    I’ve included a short tutorial below for adding an AdSense block under just the first post on your Wordpress K2 installation.

    1. Open theloop.php in your Theme Editor
    2. Find the following section of code:
      <?php } ?>
      
      <?php /*
      	The 'next page' and 'previous page' navigation for permalinks have to be inside the loop.
      	The exact opposite is true for the same navigation links on all other pages.
      	Also, we don't want them at the top of the frontpage: */
      
    3. In the gap between the php’s add the following line of code so that the section now looks like this:
      <?php } ?>
      <?php $showAdSense = True; ?>
      <?php /*
      	The 'next page' and 'previous page' navigation for permalinks have to be inside the loop.
      	The exact opposite is true for the same navigation links on all other pages.
      	Also, we don't want them at the top of the frontpage: */
    4. Now find the following section of code:
      <div class="itemtext">
      	<?php if (is_archive() or is_search()) {
      		the_excerpt();
      	} else {
      		the_content("Continue reading '" . the_title('', '', false) . "'");
      	} ?>
      <?php link_pages('<p><strong>Pages:</strong> ', '</p>', 'number'); ?>
      </div>
      
      <!--
      <?php trackback_rdf(); ?>
      -->
    5. Modify the code section above to look like this:
      <div class="itemtext">
      	<?php if (is_archive() or is_search()) {
      		the_excerpt();
      	} else {
      		the_content("Continue reading '" . the_title('', '', false) . "'");
      	} ?>
      <?php link_pages('<p><strong>Pages:</strong> ', '</p>', 'number'); ?>
      </div>
      <?If ($showAdSense) { ?>
      <div style="float:right;padding:3px;">
      	AdSense block as provided by Google
      <br /><br /><br /><br />
      </div>
      <?php $showAdSense = False;} ?>
      <!--
      <?php trackback_rdf(); ?>
      -->

    Now you can enjoy abiding by Google’s TOS rather than grossly abusing them.

    2 Responses to “WordPress K2: Google AdSense Under First Post Only”

    1. Bloghash.com Says:

      Thank you for putting up the link to my conditional display of AdSense post on your article. I am not familiar with K2 theme, but one question I have is that without counting the number of post how does it show the Ad only after the first post? I couldn’t understand how it would work by looking at your code here..

      Thanks!

    2. davemorg Says:

      Thanks for the comment/question! The key comes from the variable that is set at the beginning of the code. Before ‘theloop’ I set a custom variable:

      $showAdSense = True

      Then, after ‘theloop’ I set this same variable:

      $showAdSense = False

      In between all of this, I check this variable to see if it is True or False. After the first post, the variable gets set to False and no more adblocks are shown.

      Hopefully this makes some sense! :)

    Leave a Reply

    Note: This post is over a year and a half old. You may want to check later in this blog to see if there is new information relevant to your comment.