在 Wordpress 中将变量 php 传递给 css 的正确方法 [英] Right way for pass variable php to css in Wordpress

查看:16
本文介绍了在 Wordpress 中将变量 php 传递给 css 的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将变量从 php 传递给 css,以便从自定义主题选项自定义样式.

i need to passing variable from php to css for customize style from custom theme option.

我找到的唯一方法是创建 file.css.php并且运行良好.

The only way i have find is create file.css.php and working good.

但我的问题是:这有利于网站加载文件 .css.php 吗?或者可能有其他问题,例如速度或搜索引擎优化?

But my question is: This is good for website load a file .css.php ? or can have some other problem, like speed or seo?

还有其他一些好的方法吗?谢谢

There are some other good methods? Thx

推荐答案

间接解决方案:

听起来您想根据用户选择包含不同的 CSS 行为.假设用户选择存储在变量 $foo 中.只需像这样将它包含在元素的类中

Indirect solution:

It sounds like you want to include different CSS behavior based on user selection. Let's say that user selection is stored in a variable $foo. Just include it in an element's class like this

<?php 
$foo = 'option-1'; ?>
<div class="<?php echo $foo; ?>"></div>

<小时>

对于您的问题,还有两种直接解决方案:

1.在文件的页眉中使用内联 CSS 或 CSS:

<style>
  div.button { color:<?php echo $bar ?>; }
</style>

2.使用 PHP 文件作为 CSS.这看起来像:

<link rel="stylesheet" type="text/css" href="/style.php">

然后您就可以在您的 CSS 文件中使用 PHP 变量.只需确保将 content-type 更改回文件开头的 CSS,如下所示:

Then you can use PHP variables right inside your CSS file. Just make sure you change the content-type back to CSS at the beginning of the file like this:

<?php header("Content-type: text/css; charset: UTF-8"); ?>

这种方法有点不合常规,但它可以在没有任何速度或 SEO 缺点的情况下工作.

This method is a little bit unconventional, but it'll work without any speed or SEO drawbacks.

这篇关于在 Wordpress 中将变量 php 传递给 css 的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆