PDA

View Full Version : Help putting Adsense at Bottom of Post


JohnPlace
07-09-2007, 08:16 PM
Kevin,

You just sent me an email asking me to post my code here in your forums so you could help, so that's what I'm doing.

Essentially, what I'm trying to do is get a second AdSense ad to pop up at the bottom of each post, right-aligned. I already have an ad at the top of each post, left-aligned, which you can see by clicking here: Are You Smarter than a Caveman? (http://johnplaceonline.com/achieve-balance/are-you-smarter-than-a-caveman/)

See, here's the line of code where I suspect the problem lies (in my single.php file).

<?php the_content('<p class="serif">Read the rest of this entry &raquo;</p>'); ?>

You see, if I put the Ad above this line, it ends up at the TOP of the post. And if I put the Ad below this line, it ends up below the post, right-aligned next to the related articles link.

I want the ad to be part of the post, not beneath the post -- basically, I want it to be right aligned at the very end of the article. Certainly there must be some way to make this happen without modifying each post individually.

Any help is much appreciated.

cmanlong
07-09-2007, 08:41 PM
i could help you in blogger

Kevin
07-09-2007, 08:41 PM
im not sure if this can be done. I'm not a css guru though so hopefully im wrong :)

you have aligned the ad a the top to the left. The problem is, if you place an ad underneath the post content and align it right it will not be wrapped by the content because the content has already finished (if that makes sense). Therefore it would just align to the right on the next line ie. to the right of where your stumbleupon image is

The only way I think you could do something like this is to place the ad just after the left google ad and align it right using a relative or absolute position ie. tell the ad to align to the right 400 or 500 pixels down the page. I've never did something like this so I'm not sure how good it could be implemented and in any case I dont believe its practical because your posts will be of different length anyways.

Have you seen this done on another website?

cmanlong
07-09-2007, 09:03 PM
i know in blogger you could do a quick and dirty implementation of this using the post format editor.

JohnPlace
07-09-2007, 09:24 PM
There must be some way to hack the code in the "the_content" method to append my ad to the end of its output.

The problem is that I'm not a php programmer. I don't even know where the source for that method is hiding -- I've looked for it.

Probably hiding in some top secret API somewhere.

Kevin
07-09-2007, 09:27 PM
that code is in the wp-includes/post-template.php file.

you will see function the_content around line 50

JohnPlace
07-09-2007, 09:51 PM
that code is in the wp-includes/post-template.php file.

you will see function the_content around line 50


Wish me luck. I'll be making a backup of that file before I start tinkering, just in case my site ends up looking like spaghetti.

JohnPlace
07-09-2007, 10:00 PM
Hey, one more quick question. Do I need a special editor to modify a php file? I tried opening it in notepad and it looks all hosed up.

JohnPlace
07-09-2007, 10:29 PM
Okay, well nevermind. I downloaded a PHP editor and took a look at the code, and there's no way I'm screwing with this. I come from a microsoft background and I don't understand the syntax at all.

I stopped writing code for a living because I hated it (despite being fairly good at it), so I have no desire to learn a new language just so I can add an advertisement to the bottom of my post.


Kevin, do you have any examples of what the code looks like that accomplishes what you intended here:


The only way I think you could do something like this is to place the ad just after the left google ad and align it right using a relative or absolute position ie. tell the ad to align to the right 400 or 500 pixels down the page. I've never did something like this so I'm not sure how good it could be implemented and in any case I dont believe its practical because your posts will be of different length anyways.

Kevin
07-10-2007, 04:13 PM
If you have coded before I'm sure you would be able to edit it to do what you want. PHP is very very similar to Java and has a lot of similarities with C+ as well.

I'm not 100% sure of the code but I don't believe it would be too different from what it is now. All you are doing is positioning the ad using an absolute or relative position. This article (http://www.w3schools.com/css/pr_class_position.asp) might help you understand what i mean.

Unfortunately, I've never did something like this so I'm not sure how well it would work.

Celebrienne
07-30-2007, 03:54 PM
Do not mess with the includes or your hacks will be lost when you update to newer WordPress versions. It's not elegant to hack around or hardcore in the WordPress functionality anyway.

If I got you right, you want to place adsense at the bottom of the post, but so it floats right around your last paragraph of the post?

If so, you must understand that your post content is stored in a database, so afaik it's not possible to inject AdSense the way you want it without going through all of your posts and put the code right there.

What you could do, is install the WordPress Plugin AdSense Deluxe (http://www.acmetech.com/blog/2005/07/26/adsense-deluxe-wordpress-plugin/). It's a very powerful plugin and you can easily put AdSense in your posts by adding quick tags like <!–adsense–> instead of always adding a bunch of JS code (which won't work anyway since JS won't be executed in posts).

You could also do the following:


Open single.php
find the line with <?php the_content(); ?>
put the AdSense code beneath it wrapped in a <div style="float:right;">AdSense code here</div>
add <h3>You might also enjoy these articles:</h3><ul><?php related_posts(); ?></ul>
save single.php


This way your AdSense ad will show up above the related posts. Try it out if you like (I hope I didn't have any errors in there ;))

Good luck! :)