我想读一本书。css文件,并将其作为内联样式注入wp\\U头。有可能吗?
我试过这个“Add inline css to theme“解决方案,但它没有达到我的预期,因为我希望所有的风格都能注入
<style> [mystile.css] </style>
<link href="[mystile.css]">
我想读一本书。css文件,并将其作为内联样式注入wp\\U头。有可能吗?
我试过这个“Add inline css to theme“解决方案,但它没有达到我的预期,因为我希望所有的风格都能注入
<style> [mystile.css] </style>
<link href="[mystile.css]">
您需要将css添加到wp\\U头部。为此,您需要首先获取动态CSS,将其加载到变量中,然后将该变量添加到head输出中。就像这样:
(此代码可以进入functions.php、插件文件或主题中的其他地方,请确保将此代码包含在可以正确调用的文件中)
<?php
function so_output_dynamic_css() {
// do some stuff to get your CSS here..
$some_dynamic_css = "body {background: green;}";
?>
<style type="text/css" id="so-dynamic-css">
<?php echo $some_dynamic_css; ?>
</style>
<?php }
add_action(\'wp_head\', \'so_output_dynamic_css\');
我找到了一个不使用“file\\u get\\u contents()”就包含css文件的解决方案,因为它可能需要php。ini编辑(allow\\u url\\u fopen)。另一方面,wp\\u add\\u inline\\u style()需要输出一个注册的样式,在这种情况下,我不能使用它,因为这是折叠样式之上的样式(其余的都加载到页脚中)。
正如@user3135691所指出的,您需要在所有css中都有一个变量,然后将其附加到头部。
我的解决方案是:
function inject_css_style() {
// get the acf.css file and store into a variable
ob_start();
include \'wp-content/themes/my-theme/inline.css\';
$atf_css = ob_get_clean();
// return the stored style
if ($atf_css != "" ) {
echo \'<style id="inline-css" type="text/css">\'. $atf_css . \'</style>\';
}
}
wordpress颜色是否有CSS类:https://make.wordpress.org/design/handbook/design-guide/foundations/colors/例如,类似于2017年主题的课程:.has-text-color .has-background .has-very-dark-gray-color .has-very-light-gray-background-color