問題描述

我為 Twenty Twelve v1.0 創建了一個小孩主題,我想刪除 Open Sans 字體。

Open Sans 被添加到 Twenty 十二的 functions.php 中:

wp_enqueue_style( 'twentytwelve-fonts', add_query_arg( $query_args, "$protocol://googlefonts.admincdn.com/css" ), array(), null );

我試圖在我的 childtheme 的 functions.php(參見下面的例子) 中註銷/排除樣式表,但不起作用:

function example_scripts_styles() {
    wp_deregister_style( 'twentytwelve-fonts' );
    wp_dequeue_style( 'twentytwelve-fonts' );
}
add_action( 'wp_enqueue_scripts', 'example_scripts_styles' );

任何想法如何我可以刪除這個文件?謝謝!

最佳解決方案

找到答案 here

Script dequeuing calls should be added to the wp_print_scripts action hook(..). This is because scripts are typically enqueued on the wp_enqueue_script hook, which happens early in the wp_head process. The wp_print_scripts hook happens right before scripts are printed, and thus is latest in the process. (Otto)

遵循相同的邏輯,我們可以使用 wp_print_styles 來刪除 Open Sans 字體:

function remove_open_sans() {
   wp_dequeue_style( 'twentytwelve-fonts' );
}
add_action('wp_print_styles','remove_open_sans');

這樣做了。

次佳解決方案

在 WP 3.8+以上,我使用 thetrickster’s gist 從前端樣式成功刪除了’Open Sans’:

<?php
// Remove Open Sans that WP adds from frontend
if (!function_exists('remove_wp_open_sans')) :
    function remove_wp_open_sans() {
        wp_deregister_style( 'open-sans' );
        wp_register_style( 'open-sans', false );
    }
    add_action('wp_enqueue_scripts', 'remove_wp_open_sans');

    // Uncomment below to remove from admin
    // add_action('admin_enqueue_scripts', 'remove_wp_open_sans');
endif;
?>

‘Open Sans’ 可以是插件依賴。

第三種解決方案

在 Twenty Twelve v1.1 的 functions.php 中,註釋説明瞭如何從 wp_enqueue_scripts 鈎子中刪除樣式表:

function mytheme_dequeue_fonts() {
         wp_dequeue_style( 'twentytwelve-fonts' );
      }

add_action( 'wp_enqueue_scripts', 'mytheme_dequeue_fonts', 11 );

您的嘗試不奏效在 add_action()中缺少 priority parameter 。父主題以默認優先級為 10 的方式排列樣式,因此子主題必須以優先級 11 對齊。

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。