0

My theme automatically assign before of <!--more--> tags, as excerpt.And showing excerpt on home page. Now i want to add Google Adsense after <!-- more --> code. I'm wrote this function and add it to functions.php

function google_adsense($content)
{
    if(is_single())
    {
        global $post;
        $thePostID = $post->ID;
        $more_span = '<span id="more-' .$thePostID.'"></span>' ;
        return str_replace($more_span, $more_span . " " . google_reklam_ekle(), $content);
    }
}
function google_reklam_ekle()
{
    return "<!-- Google Adsense -->
<script type=\"text/javascript\"><!--
google_ad_client = \"ca-pub-9792205570091420\";
/* teknoblogo-yazi-alaninda */
google_ad_slot = \"0917868905\";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type=\"text/javascript\"
src=\"http://pagead2.googlesyndication.com/pagead/show_ads.js\">
</script>
<!-- bit Google Adsense -->";
}
add_filter('the_content', 'google_adsense');

Now excerpts isn't disappering excerpts. When i remove this functions, they're appearing again.

In single page, code is working very well. You can check my blog posts. http://www.teknoblogo.com . There is an Adsense Ads after tag. But before add this Adsense codes, my post's first part (i mean before of <!--more--> tag) appearing .

Can you give an idea to me ?

2 Answers 2

1

you have forgotten to return the unaltered $content if this is not a single post:

function google_adsense($content) {     
  if(is_single())     
  {         
   global $post;         
   $thePostID = $post->ID;         
   $more_span = '<span id="more-' .$thePostID.'"></span>' ;         
   return str_replace($more_span, $more_span . " " . google_reklam_ekle(), $content);     
   } else { 
   return $content; 
   }
} 
2
  • Haha! We both arrived at the same conclusion and posted it at nearly the same time!
    – tollmanz
    Commented May 15, 2011 at 17:59
  • Opps you right !!! I working on this about an hour. And i couldn't found this ! Haha ! Thank you Michael and Sardine :)
    – Eray
    Commented May 15, 2011 at 18:03
2

I think I figured this out. The problem, I believe, is that you specify your function to run only on single posts via is_single(). When it is not a single post, you return no content, therefore your content vanishes! Try this:

function google_adsense($content)
{
    if(is_single())
    {
        global $post;
        $thePostID = $post->ID;
        $more_span = '<span id="more-' .$thePostID.'"></span>' ;
        return str_replace($more_span, $more_span . " " . google_reklam_ekle(), $content);
    }
    return $content;
}

I think this will fix your vanishing content problem! It did when I ran your functions on my dev site.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.