我的 HTML 中有一個腳本鏈接
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCrAbBwliakQCs7vDgLGilKQnicEc0-8hA&callback=initMap&v=weekly" defer></script>
使用“wp_dequeue_style”時,我需要在我的 php 中添加屬性“defer”
wp_enqueue_script('google-map', 'https://maps.googleapis.com/maps/api/js?key=AIzaSyCrAbBwliakQCs7vDgLGilKQnicEc0-8hA&callback=initMap&v=weekly', [], $version, true);
有什么選擇嗎?
uj5u.com熱心網友回復:
請將此添加到您的functions.php 檔案中并再次檢查。
add_action( 'wp_enqueue_scripts', 'qcpd_wp_script_to_scripts', 20, 1);
if (!function_exists('qcpd_wp_script_to_scripts')) {
function qcpd_wp_script_to_scripts(){
wp_enqueue_script('google-map', 'https://maps.googleapis.com/maps/api/js?key=AIzaSyCrAbBwliakQCs7vDgLGilKQnicEc0-8hA&callback=initMap&v=weekly', [], '', true);
}
}
add_filter( 'script_loader_tag', 'qcpd_wp_script_defer_load_js', 100, 3 );
if (!function_exists('qcpd_wp_script_defer_load_js')) {
function qcpd_wp_script_defer_load_js( $tag, $handle, $src ) {
//$defer_load_js = true;
$handles = array(
'google-map'
);
if ( ! wp_is_mobile() && in_array( $handle, $handles ) ) {
return str_replace(' src', ' defer src', $tag);
}
return $tag;
}
}
uj5u.com熱心網友回復:
你不能這樣做,wp_enqueue_script你將不得不使用其他一些腳本加載器鉤子并添加延遲。
請查看這篇文章,它有對您有幫助的解釋和代碼。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/511128.html
標籤:phpWordPress
