简体   繁体   English

将Google Analytics添加到Wordpress的next_post_link功能

[英]Adding Google Analytics to Wordpress' next_post_link function

Working on setting up Google Analytics within a custom theme. 致力于在自定义主题中设置Google Analytics。 I make use of the previous_post_link and next_post_link functions within my site to do some navigation pieces. 我利用我网站中的previous_post_link和next_post_link函数来做一些导航。 Now I'm trying to add in some Google Analytics to those. 现在我正在尝试为这些添加一些Google Analytics。

I've added this to my functions.php file: 我已经将它添加到我的functions.php文件中:

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;
}

When I try that, I get a 500 error thrown back at me. 当我尝试这个时,我收到了500错误。 If I replace the ga('...'); 如果我更换ga('...'); junk with test , it will load and work fine. 垃圾与test ,它将加载和工作正常。

Anyone know why this is, and how I can fix it? 任何人都知道这是为什么,以及如何解决它?

Try escaping your quotes. 尝试转义你的报价。 Since you're using single quotes on str_replace() you have to escape the quotes in the function itself. 由于您在str_replace()上使用单引号,因此必须转义函数本身的引号。

 $link = str_replace('" rel="next">', '" onclick="ga(\'send\', \'event\', \'NavNext\', \'click\');" rel="next">', $link);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将PHP代码放入wordpress分页功能(previous_post_link,next_post_link等)中? - How to put PHP code in wordpress pagination function (previous_post_link,next_post_link,etc)? 仅来自当前子类别的wordpress next_post_link() - wordpress next_post_link() only from current child category Wordpress:将if-else语句插入next_post_link()/ previous_post_link()参数? - Wordpress: Inserting if-else statement into next_post_link() / previous_post_link() parameters? 如何从next_post_link和previous_post_link(Wordpress)中排除某些类别? - How to exclude some Categories from next_post_link and previous_post_link (Wordpress)? Wordpress next_post_link / previous_post_link不属于同一类别 - Wordpress next_post_link/previous_post_link not staying in same category Wordpress:previous_post_link / next_post_link按字母顺序排列? - Wordpress: previous_post_link / next_post_link by alphabetical order? WordPress-next_post_link / previous_post_link提供文本而不是链接 - Wordpress - next_post_link/previous_post_link giving text instead of links 如何在简码中显示 previous_post_link () / next_post_link () - Wordpress - How to show previous_post_link () / next_post_link () in shortcode - Wordpress WordPress-next_post_link()不按post_type过滤,什么也没显示 - Wordpress - next_post_link() not filtering by post_type showing nothing 获取next_post_link是粘性的吗? - Get next_post_link which is Sticky?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM