將下面的程式碼新增到當前主題的 functions.php 檔案:
//顯示頁面查詢次數、載入時間和記憶體佔用
function performance( $visible = false ) {
$stat = sprintf( '%d queries in %.3f seconds, using %.2fMB memory',
get_num_queries(),
timer_stop( 0, 3 ),
memory_get_peak_usage() / 1024 / 1024
);
echo $visible ? $stat : "<!-- {$stat} -->" ;
}
function performance( $visible = false ) {
$stat = sprintf( '%d queries in %.3f seconds, using %.2fMB memory',
get_num_queries(),
timer_stop( 0, 3 ),
memory_get_peak_usage() / 1024 / 1024
);
echo $visible ? $stat : "<!-- {$stat} -->" ;
}
然後可以在需要顯示的地方,使用下面的程式碼進行呼叫:
<?php if(function_exists('performance')) performance(false) ;?>
performance 的引數 false 表示在頁面前端不顯示,只在 html 原始碼中可見,如本文配圖。
如果你想在頁面中直接顯示,可改為 ture
如果你想在頁尾載入,可以將下面的程式碼新增到 functions.php 該函式的下面 (確保你的主題檔案中包含 wp_footer()):
add_action( 'wp_footer', 'performance', 20 );