它应该在它自己的样式表中吗?怎么做?(PHP在样式表?)


Should This Be In Its Own Stylesheet, and How? (PHP In Stylesheet?)

创建WordPress选项面板。它所做的一件事是允许用户选择自定义颜色。我使用默认样式表,然后调用自定义输入的样式。但是,我只是将其插入标题中,而不是让这些自定义值驻留在它们自己的样式表中(因为我需要通过PHP调用用户的选择)。

作为一个例子,我在头中有这样的代码:

<style type="text/css">
p a { <?php echo get_option('to_custom_css'); ?> }
</style>

在我的函数中:

array( "name" => "Custom CSS",  
    "desc" => "Want to add any custom CSS code? Put in here, and the rest is taken care of. This overrides any other stylesheets. eg: a.button{color:green}",  
    "id" => $shortname."_custom_css",  
    "type" => "text",  
    "std" => ""),    

我如何让它驻留在自己的样式表中,同时仍然使用<?php echo get_option('to_custom_css'); ?>调用用户输入?

您可以在PHP中创建样式表,但需要将Content-type头设置为text/css。在你的HTML中:

<head>
  <link rel="stylesheet" type="text/css" href="user-styles.php" />
  <!-- ... -->

<?php
header('Content-type: text/css');
?>
/*  now put your css here : */
p a { 
    <?php echo get_option('to_custom_css'); ?> 
}
/* and so on... */

首先,将此添加到.htaccess文件中,以便它可以解释css文件中的php:

AddType application/x-httpd-php .css

然后,链接到外部样式表。使链接动态地包含确定用户的css值(可能是id号)所需的信息,如下所示:

<link rel="stylesheet" href="http://www.yourserver.com/customstyle.css?id=<?php echo $userid; ?>" type="text/css" />

最后,将php代码放入样式表中,以便动态打印出css,如下所示:

<?php echo get_option('to_custom_css'); ?>

使用$_GET['parametername']检索允许计算css数据的参数

最终,您应该将CSS写入可以从托管页面包含的文件中。这样它就可以被浏览器缓存。

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