在将我的Google Adsense代码添加到下面的代码中时遇到问题。
<?php
function ww_header(){
echo "<!DOCTYPE html>
<html>
<head>
<title>Page title</title>
// I Want to paste my Google Adsense code here
</head>
?>
这是我的Google Adsense代码的样子
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js">
</script>
<script>
(adsbygoogle = window.adsbygoogle || []).push({
google_ad_client: "ca-pub-77XXXXXXXXXXX",
enable_page_level_ads: true
});
</script>
我尝试使用echo和include。 Adsense脚本与HTML5和php冲突,因为head标签位于php中。 有什么办法可以在不更改php和HTML位置的情况下编写Adsense代码。
答案 0 :(得分:0)
问题是您正在使用echo "
(带双引号)。您的脚本代码也有双引号。因此,您需要在script标记中转义双引号,或者对echo
使用单引号。这对您有用吗?
function ww_header(){
echo '<!DOCTYPE html>
<html>
<head>
<title>Page title</title>
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js">
</script>
<script>
(adsbygoogle = window.adsbygoogle || []).push({
google_ad_client: "ca-pub-77XXXXXXXXXXX",
enable_page_level_ads: true
});
</script>
</head>
<body>
Your content here...
</body>
</html>';
}
答案 1 :(得分:0)
<?php
function ww_header()
{
echo "<!DOCTYPE html>
<html>
<head>
<title>Page title</title>
<script async src='//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js'>
</script>
<script>
(adsbygoogle = window.adsbygoogle || []).push({
google_ad_client: 'ca-pub-77XXXXXXXXXXX',
enable_page_level_ads: true
});
</script>
</head>";
}
?>
答案 2 :(得分:0)
可能有更好的方法来执行此操作,但是只是要完全解决您的要求,请尝试以下操作:
<?php
function ww_header() {
echo '<!DOCTYPE html>
<html>
<head>
<title>Page title</title>
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js">
</script>
<script>
(adsbygoogle = window.adsbygoogle || []).push({
google_ad_client: "ca-pub-77XXXXXXXXXXX",
enable_page_level_ads: true
});
</script>
</head>';
?>