Monday 26 November 2012

How to Add Google Adsense / HTML Codes to Genesis Without A Plugin



I’ve been using Genesis for quite a while and till just a few hours ago, I didn’t know I could actually do away with the “Post Layout” plugin I’ve been using to add Google ads and other codes to properly format my post pages. Over the last few weeks, I’ve been trying to reduce my number of installed plugins in order to decrease page load time and also CPU usage. From disabling StatPress, Auto Excerpt Everywhere, down to Social Author bio and loads of other plugins, I thought it would be a good idea to use Genesis Hooks and some conditional statements to display ads and other codes instead of relying a third-party plugin I didn’t really need.
Finally, I was able to display the ads and other stuffs just the way I wanted it and right now, I’m using Genesis hooks to display everything you see above and below posts. As a matter of fact, it rather fun digging into the code side of things… got to learn more WordPress conditional statements (I never really did care because I didn’t know how useful they were).
We’re adding these adsense codes or custom PHP / HTML in five positions in this tutorial, it’s up to you to choose where you want your stuffs to appear:
1. The sidebar
2. Below the header
3. Below post title
4. After post content
5. Above posts in archive pages

1. Adding ad codes to the sidebar

Well, I’m not sure I need to explain this but for beginners, you only need to go to Appearance > Widgets. Drag the text widget to your desired location and paste the ad code or custom HTML. That’s it.

2. Adding Google Adsense below the header

I already explained this in a previous post but this is going to be a bit advanced. For someone like me who has a skycrapper on the sidebar and two ad units on post pages, I would like hide that leaderboard below the menu on post pages. This is where conditional statements come in; you only need to add a snippet of code to tell the advert not to appear on single posts and pages.
- Download and install Simple Genesis Hooks
- Go to Genesis > Simple Hooks
- Scroll down till you get to the content section
- In the box labelled genesis_before_content_sidebar_wrap Hook, paste this code:

- Be sure to mark Execute Shortcodes on this hook and Execute PHP on this hook
The part in red is the conditional statement that tells the ad not to appear on single posts and pages. So, this appears on the home page and all archive pages and with this implemented the right way, you won’t be having more than three ad units on each of your site pages. You can even take this further if you’ve done PHP programming or any sort of programming before. It’ll be much more easier for you to manipulate this statements and make your ads appear exactly where you want them to be.

3. Below post title

Now we’re going to add adsense below post title and we’re still gonna use Simple Hooks and conditional statements. The first time I did this, the Google Adsense ads appeared on archive pages and the home page till I added a statement that forced the ads to appear on single posts and pages only.
- Go to Genesis Simple hooks settings page and find genesis_before_post_content Hook
- In the box, paste this code:

- Also, mark Execute Shortcodes on this hook and Execute PHP on this hook
The part in red tells the advert to appear on single posts or pages. It’s gonna be hidden from the homepage and archive pages and I’m sure you don’t wanna show it there, do you?

4. After post content

You’re still using the same code you used right above but you’re only going to add it to a different position, OK?
- Search for genesis_after_post_content Hook
- Paste the code up there in to the box and save.
You’ll probably notice that the post meta (tags and categories) appear above the advert. You can reposition this by marking the box that says Unhook genesis_post_meta() function from this hook?.
- Below the code already in the box, now add this:

5. Above posts in archive pages

Still tweaking the conditional statements, we can make ads appear on archive pages only. This include category pages, tag pages, author pages and so on.
To make ads appear directly above posts on archive pages:
- go to Genesis Simple Hooks settings page and search for genesis_before_loop
- Paste this


0 comments

Post a Comment