![](/img/trans.png)
[英]How to put PHP code in wordpress pagination function (previous_post_link,next_post_link,etc)?
[英]Adding Google Analytics to Wordpress' next_post_link function
致力於在自定義主題中設置Google Analytics。 我利用我網站中的previous_post_link和next_post_link函數來做一些導航。 現在我正在嘗試為這些添加一些Google Analytics。
我已經將它添加到我的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;
}
當我嘗試這個時,我收到了500錯誤。 如果我更換ga('...');
垃圾與test
,它將加載和工作正常。
任何人都知道這是為什么,以及如何解決它?
嘗試轉義你的報價。 由於您在str_replace()
上使用單引號,因此必須轉義函數本身的引號。
$link = str_replace('" rel="next">', '" onclick="ga(\'send\', \'event\', \'NavNext\', \'click\');" rel="next">', $link);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.