Support Forum

Adsense code

39 Answers

New Answer

YS Yellow Swordfish
Yellow Swordfish
Member

Why thank you! I can’t offer you a better wish so I offer you the same!

CP Christian Pagan
Christian Pagan
Member

Brandon C said
Here is how you can do it.

First create your own theme so any changes you make are not over written by updates. See here on how.

http://codex.simple-press.com/codex/themes/theme-basics/creating-a-theme/

Since you want to use the adsense code in multiple paces I would create a function for it.

It appears you have seen the files in yourthemes/template folder.  One of the is spFunctions.php.  This is a file that you can add a function to, among other things, that your theme can use.

Create a function for your ad. Let’s call it spAddCode

Open the spFuntions.php file up in your editor and at the bottom just above the closing ?> is where you can add the function.

Your function should look something like this.

function spAddCode() {

class=”brush-php syntax”>spAddCode();

I am trying to add ads and this is what my function looks like 

function show_ad() {
 sp_adsense ('<script type="text/javascript"><!--
google_ad_client = "ca-pub-8654929854924223";
/* Top & Bottom */
google_ad_slot = "2182142041";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>');
}

After doing that I put this code into sp_topic_view.php:

if ( $spTopicView->current_post == 1 ) {
 echo show_ad();
 }

But my add is not showing anywhere. What am I doing wrong.

YS Yellow Swordfish
Yellow Swordfish
Member

There is no such data item as $spTopicView->current_post.

That should be $spTopicView->currentPost – no spaces, capital P

CP Christian Pagan
Christian Pagan
Member

Ok I have fixed that, but my ad is still not showing up.

MP Mr Papa
Mr Papa
Member

not sure what you are trying to do…  if you are going to ‘echo’ the ad content, the function needs to return something…  and not sure what sp_adsense() is… so something like

function show_ad() {
    return '<script type="text/javascript"><!--
            google_ad_client = "ca-pub-8654929854924223";
            /* Top & Bottom */
            google_ad_slot = "2182142041";
            google_ad_width = 728;
            google_ad_height = 90;
            //-->
            </script>
            <script type="text/javascript"
            src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
            </script>';
}
CP Christian Pagan
Christian Pagan
Member

where am I adding this in the functions file and then I need to show that ad under posts so I can add 

if ( $spTopicView->current_Post == 1 ) {

echo show_ad();

}

under spTopicView but now I get an error.

Parse error: syntax error, unexpected T_RETURN in /problem-with-post-edit-buttonome/cpagan/public_html/wp-content/sp-resources/forum-themes/mytheme/templates/spFunctions.php on line 32

CP Christian Pagan
Christian Pagan
Member

Never mind I just copied your code and it put extra numbers so now error gone but ads are still not there!!!

BR Brandon
Brandon
Member

Let’s start this kinda over again…

In your spFunctions file add your function and the code for adsense.

function show_ad(){
echo '<center><script type="text/javascript"><!--
google_ad_client = "pub-3637000718131176";
/* 728x90, created 6/1/10 */
google_ad_slot = "3102579232";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></center>';
}

Put your adsense code in between the first single quote ‘ and the last ‘ one.

Then in your spTopicView.php file add this

if ($spTopicView->currentPost == 1) {                                    
if (function_exists('show_ad')) show_ad();
}

Put that below

sp_SectionStart('tagClass=spPostContentSection', 'content');

and above

sp_PostIndexContent('', __sp('Awaiting Moderation'));

That should do it.

CP Christian Pagan
Christian Pagan
Member

I got to this part but than you lost me on put that below and this above. Below and above what???

if ($spTopicView->currentPost == 1) {
     if (function_exists('show_ad')) show_ad();
 }

put that below?????

sp_SectionStart('tagClass=spPostContentSection', 'content');

and above?????

sp_PostIndexContent('', __sp('Awaiting Moderation'));
CP Christian Pagan
Christian Pagan
Member

I mean it works but without the 2 last codes which I am not sure what they do!!

BR Brandon
Brandon
Member

Sorry, In your spTopicView.php file you will see these entries already in it.

sp_SectionStart('tagClass=spPostContentSection', 'content');
 sp_PostIndexContent('', __sp('Awaiting Moderation'));

Put this

if ($spTopicView->currentPost == 1) {
    if (function_exists('show_ad')) show_ad();
 }

between them.

OP OPShots
OPShots
Member

Awesome code example…thank you!

I got it working great…what if I wanted to add another ad say after the first and after the third or fourth post?

MP Mr Papa
Mr Papa
Member

just change your checks on the current post position…

if ($spTopicView->currentPost == 1 || $spTopicView->currentPost == 3 || $spTopicView->currentPost == 4)

or break it up into separate ‘if’ statements…