分类
PHP应用

WordPress主题如何去掉Google字体

在国内Google字体会是WordPress网站加载很慢,那么该如何移除Google字体?这里以WordPress自动主题twentyfourteen为例

查找Google字体

首先在functions.php文件搜索google,可以发现下面一段代码中包含Google字体

/**
 * Register Lato Google font for Twenty Fourteen.
 *
 * @since Twenty Fourteen 1.0
 *
 * @return string
 */
function twentyfourteen_font_url() {
    $font_url = '';
    /*
     * Translators: If there are characters in your language that are not supported
     * by Lato, translate this to 'off'. Do not translate into your own language.
     */
    if ( 'off' !== _x( 'on', 'Lato font: on or off', 'twentyfourteen' ) ) {
        $query_args = array(
            'family' => urlencode( 'Lato:300,400,700,900,300italic,400italic,700italic' ),
            'subset' => urlencode( 'latin,latin-ext' ),
        );
        $font_url   = add_query_arg( $query_args, 'https://fonts.googleapis.com/css' );
    }
    return $font_url;
} 

然后继续查找twentyfourteen_font_url函数使用的地方

// Add Lato font, used in the main stylesheet.
wp_enqueue_style( 'twentyfourteen-lato', twentyfourteen_font_url(), array(), null ); 

可以看到使用twentyfourteen-lato把Google字体加入了队列。那么想要去掉Google字体,就需要取消这个队列

移除Google字体

// Remove Lato Font
function lato_dequeue_fonts() {
    wp_dequeue_style( 'twentyfourteen-lato' );
}
add_action( 'wp_enqueue_scripts', 'lato_dequeue_fonts', 11 ); 

通过取消字体队列,来去掉Google字体,就这么简单,快去尝试吧