【问题标题】:How can I create inline CSS in a WordPress PHP file without referencing to an external CSS file?如何在不引用外部 CSS 文件的情况下在 WordPress PHP 文件中创建内联 CSS?
【发布时间】:2012-12-23 01:06:46
【问题描述】:

我想直接从 Wordpress 插件中加入 CSS 字符串。我不想从外部文件加载它,也不想争论为什么这是写入或错误的做法,我只想知道是否可能。

换句话说,我知道我可以做到:

wp_register_style('myStyleSheet', 'my-stylesheet.php');
wp_enqueue_style( 'myStyleSheet');

但我不想。

我想做的是这样的(伪代码):

$number = get_option('some_width')/2;
$css = " .divbox { width: $number; } ";
wp_register_style('myStyleSheet', $css);
wp_enqueue_style( 'myStyleSheet');

我阅读了 wp_register_style() 的 wp codex,但似乎不可能。有人有建议吗?

【问题讨论】:

    标签: php css wordpress


    【解决方案1】:

    那是我的愚蠢。几分钟后我找到了答案。绝望是一个很好的动力! :)

    诀窍是不要使用 wp_register_style() 和 wp_enqueue_style()

    这是我所做的:

    function myStyleSheet() {
    
      global $value;
      $num = $value/2;
    
      echo '
           <style type="text/css">
                .divbox { width: '.$num.'; }    
           </style>
        ';
    }
    add_action( 'wp_print_styles', 'myStyleSheet' );
    

    不过,也许还有更好的方法?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-13
      • 2015-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-26
      • 1970-01-01
      相关资源
      最近更新 更多