1

几天来,我一直在互联网上寻找一种动态创建 less 文件的方法,我会将 wordpress 主题定制器 api 中设置的颜色和字体变量传递到该文件中。我想出的最接近的方法是将这些变量动态插入到 style.css.php 文件中,然后将其嵌入为 css,感谢这篇css-tricks 文章。我创建了一个 php 文件,在其中编写了以下代码:

<?php
$absolute_path = explode('wp-content', $_SERVER['SCRIPT_FILENAME']);
$wp_load = $absolute_path[0] . 'wp-load.php';
require_once($wp_load);

$dark_red = get_option('color-1');
$light_red = get_option('color-2');
$white = get_option('color-3');
$dark_grey = get_option('color-4');
$light_grey = get_option('color-5');

header('Content-type: text/css');
header('Cache-control: must-revalidate');
?>
body{
    font-family: <?php echo get_theme_mod( 'theme_font' ); ?>;
}
header{
    background-color: <?php echo $dark_red ?>;
}
etc.

当它嵌入为css时有效,但是当我尝试@import“style.css.php”时;进入 bootstrap.less,就在 @import "variables.less" 的下方;要覆盖引导变量,prepros 无法重新编译 css 文件,因为输入无法识别,当我在 functions.php 中激活 lessphp 时,它给了我这个错误:

Fatal error: Uncaught exception 'Less_Exception_Chunk' with message 'ParseError: Unexpected input in custom-styles.css.php on line 1, column 0 1| @font-family-sans-serif: <?php echo get_theme_mod( 'theme_font' ); ?>;' in C:\xampp\htdocs\inicijativa\wp-content\themes\liberter-less\less\lib\Less\Parser.php:535 Stack trace: #0 C:\xampp\htdocs\inicijativa\wp-content\themes\liberter-less\less\lib\Less\Parser.php(343): Less_Parser->GetRules('C:/xampp/htdocs...') #1 C:\xampp\htdocs\inicijativa\wp-content\themes\liberter-less\less\lib\Less\Tree\Import.php(277): Less_Parser->parseFile('C:/xampp/htdocs...', '', true) #2 C:\xampp\htdocs\inicijativa\wp-content\themes\liberter-less\less\lib\Less\Tree\Import.php(193): Less_Tree_Import->ParseImport('C:/xampp/htdocs...', '', Object(Less_Environment)) #3 C:\xampp\htdocs\inicijativa\wp-content\themes\liberter-less\less\lib\Less\Tree\Ruleset.php(248): Less_Tree_Import->compile(Object(Less_Environment)) #4 C:\xampp\htdocs\inicijativa\wp-content\themes\liberter-less\less\lib\Less\Tree\Rulese in C:\xampp\htdocs\inicijativa\wp-content\themes\liberter-less\less\lib\Less\Parser.php on line 535

有没有办法将主题定制器生成的php变量插入到less中?

编辑:我似乎在这里过头了。我查看了 lessphp 文档,但似乎没有得到它。

这就是我在functions.php中设置lessphp函数的方式

    //compile less
function autoCompileLess() {

    // include lessc.inc
    require_once( get_template_directory().'/less/lessc.inc.php' );

    // input and output location
    $inputFile = get_template_directory().'/less/bootstrap.less';
    $outputFile = get_template_directory().'/style.css';

    // load the cache
    $cacheFile = $inputFile.".cache";

    if (file_exists($cacheFile)) {
        $cache = unserialize(file_get_contents($cacheFile));
    } else {
        $cache = $inputFile;
    }

    $less = new lessc;
    $less->setVariables(array(
      "font" => get_theme_mod( 'theme_font' )
    ));
    // create a new cache object, and compile
    $newCache = $less->cachedCompile($cache);

    // output a LESS file, and cache file only if it has been modified since last compile
    if (!is_array($cache) || $newCache["updated"] > $cache["updated"]) {
        file_put_contents($cacheFile, serialize($newCache));
        file_put_contents($outputFile, $newCache['compiled']);
    }
}

if(is_admin()) {
    add_action('init', 'autoCompileLess');
}

现在我需要用主题定制器设置的变量@import文件,在bootstrap.less中的variables.less下面覆盖默认的引导变量,然后我需要重新编译style.css

那么,我怎样才能 @font-family-sans-serif: <?php echo get_theme_mod( 'theme_font' ); ?>;以一种适用于lessphp的方式传递这样的东西呢?

4

0 回答 0