If I wanted to display an adsense banner on the home page, but only after the most recent post, what would i need to wrap the adsense code in?
If I wanted to display an adsense banner on the home page, but only after the most recent post, what would i need to wrap the adsense code in?
I did something similar once for some thing.....
Here's how I did it:
1) Open your index.php file. Look for some code similar to this:
<?php if ($posts) : foreach ($posts as $post) : start_wp(); ?>
This is the start of the WP-Loop.
2) Just BEFORE that line, insert this code:
<?php $showAdSense = True; ?>
3) find this code:
<?php endforeach; else: ?>
This is the bottom of the loop.
4) Just BEFORE that line, put this:
< If ($showAdSense) { ?> ... stick the adsense code here ... <?php $showAdSense = False;} ?>
Making the appropriate changes (ie, inserting the adsense code where noted.
5) Save file.
6) Sit back and enjoy.
TG
worked like a charm.
had to put a ?php before If in the first line, but other than that...
Thanks.
Top stuff - I've just implemented this on my site. As Jeremy says, it needs an extra ?php at the start of 4).
Thanks, TechGnome!
Thanks! That's what I am looking for! :-D
By the way, I modify it abit to make the inserted code(adsense ads) displayed randomly in the loop.
1. BEFORE <?php foreach ($posts as $post) : start_wp(); ?>:-
<?php
$counter = 1;
$showed = false;
$postperpage = 7;
$showAdSense = rand(1, $postperpage);
?>
2. BEFORE <?php endforeach; ?> :-
<?php
if(($counter==$showAdSense) && $showed==false) {
$showed = true;
?>
ADSENSE CODE HERE
<?php
//$showAdSense = False;
} else {
$counter++;
}
?>
See it in action: http://www.liewcf.com/wp
Also a plugin: http://jason.goldsmith.us/2004/06/23/moosecandy-120/
this isn't working for me in my wp1.5 blog. has anyone made it work with the latest version?
TechGnome's solution is working great for me, but is there a way to stipulate that the ad should appear after, say, the xth post as opposed to only after the first? (using x as a variable, here)
sure thing...before the Loop:
$postnum = 1;
$showadsense = 7; // the post you want it to display with
In the loop:
<?php if ($postnum == $showadsense) { **Adsense Code here** } ?>
And right before the end of the loop,
$adsense++;
Hi Jalenack,
When I try implementing your suggestion, I just get the error:
" parse error, unexpected '<'"
Perhaps I am putting it in the wrong place?
Was your code an addition or replacement to TechGnomes?
Thanks,
-Tut
Well, mine was somewhat of an addition to TechGnomes. Same instructions, different code. I'll just *copy & paste* his instructions with the necessary replacements:
1) Open your index.php file. Look for some code similar to this:
<?php if ($posts) : foreach ($posts as $post) : start_wp(); ?>
This is the start of the WP-Loop.
2) Just BEFORE that line, insert this code:
<?php
$postnum = 1;
$showadsense = 7;
?>
3) find this code:
<?php endforeach; else: ?>
This is the bottom of the loop.
4) Just BEFORE that line, put this:
<?php if ($postnum == $showadsense) { ?> ... stick the adsense code here ... <?php } $postnum++; ?>
Making the appropriate changes (ie, inserting the adsense code where noted. the $showadsense variable can be changed from '7' to whatever post you want it to display after.
5) Save file.
6) Sit back and enjoy.
Thank you for this! I found it very useful as I like to "blend" my ads with the rest of my blog content! :)
How could this be edited to show the ad after 1st and 2nd or 1st, 2nd and 3rd posts? I tried tinkering and just broke things.
what directory is the index.php file that needs to be edited?
It's the index.php in your theme editor - could also be called 'main template'
I'd recommend learning a bit more about the vehicle which is WP before tinkering under the hood, assuming you know where the hood is ;-). Have you even read the "manual"? http://codex.wordpress.org/Main_Page
You might find this portion helpful in your current situation:
http://codex.wordpress.org/Site_Architecture_1.5
Yeah, Beel, I read the f*ing manual. I already know that this is the index.php in the Theme folder aka Main Template. I tried using an elseif statement and it didn't work. I tried adding a second iteration of this using <?php if ($postnum == 2...etc. and that didn't work. I tried adding a second variable called "showadsenseagain" and then `<?php if ($postnum == $showadsenseagain) ..." and that didn't work. I didn't see a damn thing in the Site Architecture of WordPress that really answered the question of how to modify this little routine to output the code a second time after the second post, but thanks anyway.
Testy aren't we? Check the thread a bit more closely and you might see my comment had nothing to do with you. Then you might check out David Chait's CG-Inbetween. That should work for you. There are also threads you could search for where we posted how to add counters, etc., to the code to do what you want (but David's plugin makes life easier).
Yeah, I guess so. I was the one that mentioned tinkering, so it looked like you were responding to me.
What about AdSense BEFORE first post only... anyone know how that would work?
Exactly the same except you'd put the code before the_content instead of after the_content.
nevermind, justed placed AdSense code above the loop, under the content div
Very valuable information, thanks!
I wonder if it is possible to display an image or a banner instead of adsense...
/Erico
Yeah. It's also possible to display other types of ads like Adbrite or Yahoo Publisher Network. Instead of Adsense code, think "Advertising code".
It works very well. Thanks!
/erico
This works great! Is there a way to put a </br> immediately after the adsense block? Mine looks a little cramped.
guys,
i cannot find this line anywhere in the index.php
I have 1.5.2, is it normal?
I can't find the LOOP line, i'm using Blix..and it seems that the loop is either in another file or written in another, anyone can help?
Heat
I'm using the latest wordpress and Blix too. The code for the php seems depricated or something. Try this: Before "<?php while (have_posts()) : the_post(); ?>" insert
<?php $showAdSense = True; ?>
Now, before "<?php endwhile; ?>" insert
<?php if ($showAdSense): ?>
your adsense code
<?php $showAdSense = False; ?>
<?php endif; ?>
That should work. Hope this helps.
what if you like it to show on 1st, 2nd and 3rd post?
This topic has been closed to new replies.