將下面的代碼添加到當前主題的 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 );