Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 ?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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; 
   }
} 
share|improve this answer
    
Haha! We both arrived at the same conclusion and posted it at nearly the same time! –  tollmanz May 15 '11 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 May 15 '11 at 18:03

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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