WordPress-如何检查CSS文件名是否包含”.min”?

我想创建一个动态检查, 以查看我的CSS文件是否包含缩小版本的style.min.css而不是style.css。

我已经在生产环境中创建了开发和生产模式, 它生成了一个添加了哈希值的缩小版本, 并且该开发只是不带.min的常规未缩小

我怎样才能做到这一点?如果我有多个CSS文件, 又不想一遍又一遍地复制粘贴该怎么办?

<?php

function nm_enqueue_style() {
    wp_enqueue_style('nm-style', get_stylesheet_uri());
    $cssFilePath = glob(THEME_DIR . '/build/css/prestigexotics.min.*');
    $cssFileURI = THEME_DIR_CSS . '/' . basename($cssFilePath[0]);

    if (strpos($cssFileURI, 'min') == false) {
        wp_enqueue_style('theme', THEME_DIR_CSS . '/prestigexotics.css', array());
    } else {
        wp_enqueue_style('theme', $cssFileURI);
    }
}   
add_action('wp_enqueue_scripts', 'nm_enqueue_style');

#1


你可以创建一个适合你的函数。它将返回你需要的数组。

<?php

function checkCssMin($filePath) {
    $cssFilePath = glob(THEME_DIR . $filePath);
    $cssFileURI = THEME_DIR_CSS . '/' . basename($cssFilePath[0]);
    if (strpos($cssFileURI, 'min') == false) {
        return [false, $cssFileURI];
    } else {
        return [true];
    }
}

function nm_enqueue_style() {
    wp_enqueue_style('nm-style', get_stylesheet_uri());
    wp_enqueue_style('theme', checkCssMin('/build/css/prestigexotics.min.*')[0] ? checkCssMin('/build/css/prestigexotics.min.*')[1] : ('/prestigexotics.css', array()));
}   
add_action('wp_enqueue_scripts', 'nm_enqueue_style');

注意:我不太习惯PHP语法, 但是希望该逻辑可以对你有所帮助。我相信你可以进一步简化它。


#2


解决了该问题, 创建了一个动态输出JS文件的功能。

来源:

https://www.srcmini02.com/64362.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_37470.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?