将Google Analytics(分析)添加到Wordpress的next_post_link函数 [英] Adding Google Analytics to Wordpress' next_post_link function
问题描述
致力于在自定义主题中设置Google Analytics(分析).我利用网站中的previous_post_link和next_post_link函数进行一些导航.现在,我试图在其中添加一些Google Analytics(分析).
Working on setting up Google Analytics within a custom theme. I make use of the previous_post_link and next_post_link functions within my site to do some navigation pieces. Now I'm trying to add in some Google Analytics to those.
我已将其添加到我的functions.php文件中:
I've added this to my functions.php file:
add_filter('next_post_link', 'ga_next_post_link');
function ga_next_post_link($link) {
$link = str_replace('" rel="next">', '" onclick="ga('send', 'event', 'NavNext', 'click');" rel="next">', $link);
return $link;
}
add_filter('previous_post_link', 'ga_previous_post_link');
function ga_previous_post_link($link) {
$link = str_replace('" rel="last">', '" onclick="ga('send', 'event', 'NavLast', 'click');" rel="last">', $link);
return $link;
}
当我尝试这样做时,我又遇到了500错误的提示.如果我将ga('...');
垃圾替换为test
,它将加载并正常工作.
When I try that, I get a 500 error thrown back at me. If I replace the ga('...');
junk with test
, it will load and work fine.
任何人都知道这是为什么,我该如何解决?
Anyone know why this is, and how I can fix it?
推荐答案
尝试转义引号.由于您在str_replace()
上使用单引号,因此必须对函数本身的引号进行转义.
Try escaping your quotes. Since you're using single quotes on str_replace()
you have to escape the quotes in the function itself.
$link = str_replace('" rel="next">', '" onclick="ga(\'send\', \'event\', \'NavNext\', \'click\');" rel="next">', $link);
这篇关于将Google Analytics(分析)添加到Wordpress的next_post_link函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!