I am trying to insert Author meta ie Google adsense Publisher ID and Adunit slot number in functions.php
for wordpress. The code am using displays this metas as text eg xxxxxxxxxxxxxxxxxxx ie both the ID and slot number in same line on the web page and does not show the google adsense script/ad unit.
CODE
//RECTANGLE Adsense UNit
function get_rectangle() {
global $post; $author_id=$post->post_author;
if(is_single()) {
$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>';
} else {
$rec_Ad.= 'data-ad-client="ca-pub-xxxxxxxxxxxxxxx"';
$rec_Ad.= 'data-ad-slot="xxxxxxxxxxxxxxxxxx"></ins>';
}
$rec_Ad.= '<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>';
}
return $rec_Ad;
}
MAIN FOCUS
$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>';
The code is supposed to display in single.php page
, the ad unit of author if it exists else show the default ad unit.
THe solution can be found in the link below. How Can I return adsense code from a function in wordpress credit to @Quack.
Change
$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>';
to
$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>';
}
USE get_the_author_meta
and NOT the_author_meta
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.