如何从wordpress中的函数返回adsense代码

时间:2016-06-25 06:40:02

标签: php wordpress

从作者元调用发布者ID,即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>';
   } 

以上代码返回发布商ID,但不返回整个adsense代码

谢谢

1 个答案:

答案 0 :(得分:1)

试试这个:

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>';
}