Google Adsense要求在<head>
站点的标记。
然而,我使用的是WordPress子主题。
如何将我的Google Adsense代码粘贴到子主题中?
Google Adsense要求在<head>
站点的标记。
然而,我使用的是WordPress子主题。
如何将我的Google Adsense代码粘贴到子主题中?
您可以轻松地将您的Google Adsense代码放入<head>
在您的functions.php
文件或为此创建插件:
/**
* Load my Google Adsense code.
*
* Here, your Google Adsense Code is stored within in your plugins directory, under the
* the file named: gads.js, which contains your Google Adsense Code.
* The full file path for the purpose below: wp-content/plugins/my-plugin-name/inc/gads.js
*/
function load_my_google_adsense_code() {
wp_enqueue_script( \'gads\', plugins_url(\'/inc/gads.js\', __FILE__), null, \'\', true );
}
add_action( \'wp_enqueue_scripts\', \'load_my_google_adsense_code\' );
或者,您可以直接将其作为功能的一部分,如下所示;/**
* Load my Google Adsense code.
*
* Here, your Google Adsense Code is contained directly in your function.
*/
function load_my_google_adsense_code() {
?>
<script>
// Your Google Adsense Code here.
</script>
<?php
}
add_action(\'wp_head\', \'load_my_google_adsense_code\');
请注意:上述两(2)种方法将使您能够在<head>
WordPress网站的元素,通常在</head>
, 但这实际上取决于主题,因为它取决于wp_head()
钩在header.php
.
一般建议<?php wp_head(); ?>
在</head>
这解释了上述观点,旨在避免与其他插件之间的潜在冲突,实现如下:
<?php
/**
* The header for our theme
* File name: header.php
*/
?><!DOCTYPE html>
<html>
<head>
<!-- Other contents here -->
<?php wp_head(); ?>
</head>
<!-- Other contents may follow -->
因此,如果您的wp_head()
你的主题的钩满足那个条件:发生在你的<head>
要素如果情况并非如此,并且您仍然需要按照Google Adsense的要求实现这一点,那么您需要直接编辑您的header.php
为此目的,请:
1-将wp_head()
在你的<head>
元素如下所示:
<?php
/**
* The header for our theme
* File name: header.php
*/
?<!DOCTYPE html>
<html>
<head>
<?php wp_head(); ?>
<!-- Other contents here -->
在上述示例中,您必须使用上述两种枚举方法中的任何一种,以便利用此编辑来实现您的目标或
2-将代码直接粘贴到header.php
文件,就在您的<head>
元素如下所示:
<?php
/**
* The header for our theme
* File name: header.php
*/
?<!DOCTYPE html>
<html>
<head>
<!-- Your Google Adsense code here, right after the <head> element -->
<!-- Other contents here -->
在进行此类编辑之前,请始终记住备份文件在函数中使用wp\\U head hook。php
add_action(\'wp_head\',\'add_adsense\');
add_action( \'wp_head\', \'add_google_adsense_code\' );
function add_google_adsense_code() {
?><script>
// Your Google Adsense Code here.
</script><?php
}
请参见action hooks添加到您的子主题函数文件中,此钩子在wp\\u head之后激发,因此在关闭后打印您的脚本</head>
标签
有人知道有没有一个WP插件可以显示你的广告统计数据(比如点击量、收入等)?我一直在四处寻找,我找到的大多数插件只让你管理实际的广告代码。我发现this one 但它不起作用。提前感谢!