// add google analytics to footer
function add_google_analytics() { ?>
if ( !is_user_logged_in() ) {
<!--Google Analytics-->
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push([\'_setAccount\', \'UA-XXXXXXXX-X\']);
_gaq.push([\'_trackPageview\']);
_gaq.push([\'_trackPageLoadTime\']);
(function() {
var ga = document.createElement(\'script\'); ga.type = \'text/javascript\'; ga.async = true;
ga.src = (\'https:\' == document.location.protocol ? \'https://ssl\' : \'http://www\') + \'.google-analytics.com/ga.js\';
var s = document.getElementsByTagName(\'script\')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
}
<?php }
add_action(\'wp_footer\', \'add_google_analytics\');
尝试创建一个功能,根据用户是否登录WordPress添加google analytics跟踪代码。现在,跟踪代码一直在添加。
SO网友:petermolnar
您有?>和php在错误的地方,这样用户登录测试就是一个简单的文本。尝试以下操作:
// add google analytics to footer
function add_google_analytics() {
if ( !is_user_logged_in() )
{
?>
<!--Google Analytics-->
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push([\'_setAccount\', \'UA-XXXXXXXX-X\']);
_gaq.push([\'_trackPageview\']);
_gaq.push([\'_trackPageLoadTime\']);
(function() {
var ga = document.createElement(\'script\'); ga.type = \'text/javascript\'; ga.async = true;
ga.src = (\'https:\' == document.location.protocol ? \'https://ssl\' : \'http://www\') + \'.google-analytics.com/ga.js\';
var s = document.getElementsByTagName(\'script\')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<?php
}
}
add_action(\'wp_footer\', \'add_google_analytics\');