几天来,我一直在互联网上寻找一种动态创建较少文件的方法,我会在wordpress主题定制器api中设置颜色和字体变量。我想出的最接近的方法是动态地将这些变量插入到style.css.php文件中,然后将其嵌入为css,这要归功于this 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&#34; style.css.php&#34 ;;进入bootstrap.less,就在@import&#34; variables.less&#34;;为了覆盖引导变量,由于无法识别的输入,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变量插入到更少?
编辑:我似乎在这里过得很开心。我查看了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下面覆盖默认的bootstrap变量,然后我需要重新编译style.css
那么,我怎么能传递这样的东西呢
@font-family-sans-serif: <?php echo get_theme_mod( 'theme_font' ); ?>;
以某种方式与lessphp一起使用?