MyBB Hacks

Full Version: Hardcode Adsense code into post #1 and post #3
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I would like to know how to modify templates, to add manually Adsense <script> </script> codes specifically into the first and third post displayed on any page within a thread.

I have tried using the Conditionals plugin to see whether the post number is #1, and if so, it displays Adcode1, if it is post #3, then Adcode2.

There is a problem. I want ads to display on page 2, 3 etc too. If I got for the code which checks post number, then the ads display only on page 1!, I want them on subsequent pages too, serially on the first and third post on every page.

Here's the code I have tried so far.

Code:
<if $postcounter == '2' then>
<tr><div align="center">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-";
/* InthreadPost#2 */
google_ad_slot = "";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>
</tr>
</if>

<if $postcounter == '3' then>
<tr>
<div align="center">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-cy1";
/* Inpost#1 */
google_ad_slot = "";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>


This displays ads on Post #1 and Post #2 of page 1. I want ads on Page 2, 3 etc (all pages)

You could just subtract the page*perpage from the postcounter, though I don't know how accurate that is.
Maybe there is a string that handles the postnumber of the first displayed post in a page? In that case I could add the required number to it, then do an if like I did previously..

Where can I get full documentation of the if plugin?
I don't believe so.  The <if> is pretty much your standard PHP if in a slightly different syntax.

The postbit code is in inc/functions_post.php if you want to look at it.

Probably just do something like:

Code:
<if in_array($postcounter - $mybb->settings['postsperpage']*($page-1), array(1,3)) then>

This will display an ad on every 3rd post.  I believe that google will only let you show 3 ads (or link units) per page, and will just not show more ads after that.  The modulus operator gives the remainder of the division, so $postcounter % 2 is the amount left over when dividing by 2

Code:
<if $postcounter %2 == 0 then>
<tr><div align="center">

<!--adsense code-->

</div></tr>
</if>




I personally don't like showing too many ads to members, so use the code below.  It says that if the user is an admin or a guest then show them the ads

Code:
<if $postcounter %2 == 0 then>
<if $mybb->user['usergroup'] == '1' OR $mybb->user['usergroup'] == '4' then>
<tr><div align="center">

<!--adsense code-->

</div></tr>
</if>
</if>

"%2" will be for every second post I believe.
So if $postcounter is 1 for the first post, it will show on the 2nd, 4th, 6th etc post.
What's the format of elseif with this plugin?

Code:
<if condition then>
...
<elseif condition then>
...
<elseif condition then>
...
<else>
...
</if>

All parts except the first and last lines being optional.

Thanks for all your help. Solved it. Smile

Code:
<if in_array($postcounter - $mybb->settings['postsperpage']*($page-1), array(1,3)) then>
<span style="margin-left: 25%; margin-right: 25%;\padding: 1px;">
<if in_array($postcounter - $mybb->settings['postsperpage']*($page-1), array(1)) then>
	<tr><div align="center">
	<script type="text/javascript">
	/* ad #1*/
	
	</script>
	<script type="text/javascript"
	src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
	</script>
	</div></tr>
<elseif in_array($postcounter - $mybb->settings['postsperpage']*($page-1), array(3)) then>
	<tr><div align="center">
	<script type="text/javascript">
/* add #2 */
	</script>
	</div></tr>
</if>
 </span>
 </if>

Reference URL's