FacebookTwitter

How to Add Google AdSense To Your Posts in Genesis

By on Dec 14, 2010 in Genesis |

Share On GoogleShare On FacebookShare On Twitter

With the Genesis theme framework, adding “blocks of code” and placing them into certain areas on a site can easily be done with action hooks. The following tutorial will walk you through how to add Google AdSense to your posts.

Below you will see a screenshot that will show you the result of this tutorial:

adsense posts

Step #1

The first thing you’ll want to do is establish the CSS that you’ll need for the AdSense block. Inside your child theme’s style.css file, add this code:

/***** AdSense Post ********************/

.adsense-post {
    float: right;
    margin: 0 0 10px 10px;
    }

This piece of code will create a container that will float to the right and be wrapped by the post content. If you want the AdSense block to be floated left, simply change the code to this:

/***** AdSense Post ********************/

.adsense-post {
    float: left;
    margin: 0 10px 10px 0;
    }

Step #2

Next, you’ll want to create a file where you will place your AdSense code. You will also need to define the container for the code as well, so you create a file called adsense-post.php and place this into it:

<div class="adsense-post">
    <script type="text/javascript"><!--
    google_ad_client = "pub-4010093622692910";
    /* 250x250, created 7/27/10 */
    google_ad_slot = "3220529950";
    google_ad_width = 250;
    google_ad_height = 250;
    //-->

    </script>
    <script type="text/javascript"
    src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
    </script>
</div>

Obviously you will replace the sample AdSense code with your own.

Step #3

Now that you have defined the style and created the php file for your AdSense code, you have to establish a way to implement it into your site. Open your child theme’s functions.php file and find this code:

<?php
// Start the engine
require_once(TEMPLATEPATH.'/lib/init.php');

Immediately after that, place the code below:

// Place Google AdSense code into post
add_action('genesis_before_post_content', 'include_adsense_post', 9);
function include_adsense_post() {
    if ( is_single() )
    require(CHILD_DIR.'/adsense-post.php');
}

The code above creates a function that says “if this is a single post, hook the adsense-post.php file just above the post content”. Because you defined your Google AdSense container to float: right, this allows the post to wrap itself around your box.