2013-11-28 38 views
0

我已經安裝在我的網站上W3的總緩存插件錯誤,並出現了最近這個錯誤:wp_footer();所謂的身體之前,但W3的總緩存顯示

您的活動主題:

·有呼叫,但它不是直接調用的結束標記

之前,這是我的footer.php歸檔的結尾:

<script type="text/javascript"> 
    var _gaq = _gaq || []; 
    _gaq.push(['_setAccount', 'UA-XXXXXXXXX-X']); 
    _gaq.push(['_trackPageview']); 
    (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 wp_footer(); ?> 
</body> 
</html> 

正如你所看到的wp_footer();緊挨着</body>標籤之前。我錯過了什麼嗎?

謝謝你的幫助。

回答

0

我並不意味着與「codehorse」的回答,但還是干擾:

wp_footer()是應該高於功能,Wordpress codex明確規定「立即將這個模板標籤在標籤之前一個主題模板(例如footer.php,index.php)「。

依我拙見,正確的代碼將是這一個:

<?php wp_footer(); ?> 
    <script type="text/javascript"> 
     Here goes your Google analytics code 
    </script> 

    </body> 
</html> 

所以,你看到的。 「wp_footer」高於你的Ganalytics代碼,在這種情況下,什麼都不會搞砸。甚至有專門用於插入谷歌分析的小型WP插件,但它適用於懶惰的人,嘿嘿

+0

您不應該直接在WordPress中調用'

0

你不應該直接在WordPress中調用<script>標籤。相反,使用wp_enqueue_script()

google-analytics.js

var _gaq = _gaq || []; 
_gaq.push(['_setAccount', 'UA-XXXXXXXXX-X']); 
_gaq.push(['_trackPageview']); 
(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);})(); 

然後,在你的主題functions.php文件或插件文件:

add_action('wp_enqueue_scripts', 'so20272587_add_analytics'); 
function so20272587_add_analytics() { 
    $handle = 'google-analytics'; 
    $src = 'path/to/google-analytics.js'; // where your JS file lives 
    $deps = array(); // add any dependencies' handles in here 
    $ver = false; // you can leave this false, or define your own version # 
    $in_footer = true; // if you want to load it in the footer 
    wp_enqueue_script($handle, $src, $deps, $ver, $in_footer); 
} 

那麼你的谷歌Analytics(分析)腳本將加載您網站的頁腳的一部分。

相關問題