How Can I return adsense code from a function in w

2019-09-12 11:25发布

Am calling the publisher id from the authors meta i.e the_author_meta('pub-id', $author_id

CODE

 //RECTANGLE Adsense UNit
function get_rectangle() {
    global $post; $author_id=$post->post_author;
$rec_Ad.= '<div>';
            $rec_Ad.= '<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
            <!-- 300x250 -->
            <ins class="adsbygoogle"
                 style="display:inline-block;width:300px;height:250px"';

    if (get_the_author_meta('rectangle', $author_id)) { 

                 $rec_Ad.= 'data-ad-client="ca-pub-'.the_author_meta('pub-id', $author_id).'"';
                 $rec_Ad.= 'data-ad-slot="'.the_author_meta('rectangle', $author_id).'"></ins>';
}
            $rec_Ad.= '<script>
            (adsbygoogle = window.adsbygoogle || []).push({});
            </script></div>';
   } 

The above code returns the publisher id but not the whole adsense code

Thank you

标签: php wordpress
1条回答
看我几分像从前
2楼-- · 2019-09-12 11:59

Try this:

function get_rectangle()
{
    global $post; $author_id= $post->post_author;

    $rec_Ad = '<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>';

    <!-- 300x250 -->
    $rec_Ad .= '<ins class="adsbygoogle" style="display:inline-block;width:300px;height:250px"';

    if (get_the_author_meta('rectangle', $author_id)) {
        $rec_Ad .= 'data-ad-client="ca-pub-'. get_the_author_meta('pub-id', $author_id) .'"';
        $rec_Ad .= 'data-ad-slot="'. get_the_author_meta('rectangle', $author_id) .'"></ins>';
    }

    $rec_Ad .= '<script>(adsbygoogle = window.adsbygoogle || []).push({});</script>';
}
查看更多
登录 后发表回答