猿问

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');


慕码人2483693
浏览 119回答 2
2回答

qq_遁去的一_1

你可以创建一个function适合你的。它会返回一个array你需要的东西。<?phpfunction checkCssMin($filePath) {&nbsp; &nbsp; $cssFilePath = glob(THEME_DIR . $filePath);&nbsp; &nbsp; $cssFileURI = THEME_DIR_CSS . '/' . basename($cssFilePath[0]);&nbsp; &nbsp; if (strpos($cssFileURI, 'min') == false) {&nbsp; &nbsp; &nbsp; &nbsp; return [false, $cssFileURI];&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; return [true];&nbsp; &nbsp; }}function nm_enqueue_style() {&nbsp; &nbsp; wp_enqueue_style('nm-style', get_stylesheet_uri());&nbsp; &nbsp; wp_enqueue_style('theme', checkCssMin('/build/css/prestigexotics.min.*')[0] ? checkCssMin('/build/css/prestigexotics.min.*')[1] : ('/prestigexotics.css', array()));}&nbsp; &nbsp;add_action('wp_enqueue_scripts', 'nm_enqueue_style');注意:我不是很习惯 PHP 语法,但我希望逻辑可以帮助你。我相信你可以进一步简化它。

慕慕森

解决了创建一个函数来动态输出 JS 文件的问题。
随时随地看视频慕课网APP
我要回答