【问题标题】:Adding dynamic custom styles with priority not working in wordpress添加优先级的动态自定义样式在wordpress中不起作用
【发布时间】:2017-11-27 13:55:29
【问题描述】:

我正在尝试开发一个 Wordpress 主题,并且我正在尝试在我的主题中添加选项来自定义默认样式。为此,我添加了一个函数,该函数使用wp_enqueue_scripts() 获取用户定义的值并在头部插入定义的样式

 function woo_wonder_custom_style(){

     $custom_background_color = get_option('ww_background_color');

 ?>

    <style type="text/css">
        body{
             background-color: <?php echo $custom_background_color; ?>;
        }  
   </style>

<?php

}

add_action( 'wp_enqueue_scripts', 'woo_wonder_custom_style', 9999 );

我还有其他默认样式表,它们也包含在主题中,仅使用 wp_enqueue_scripts()。

 function woo_wonder_stylesheets()
 {
    wp_enqueue_style( 'bootstrap', get_template_directory_uri() . '/assets/css/bootstrap.min.css' );
    wp_enqueue_style( 'ww_custom', get_template_directory_uri() . '/assets/css/woo-wonder.css' );
 }

 add_action( 'wp_enqueue_scripts', 'woo_wonder_stylesheets', 11 );

我想将自定义样式 woo_wonder_custom_style() 的优先级设置为低于默认样式表 woo_wonder_stylesheets()

非常感谢任何帮助。

这是我第一次尝试使用 wordpress 主题开发,如果我犯了任何愚蠢的错误,请原谅。

谢谢

【问题讨论】:

  • 你最好将你的函数挂接到 wp_head 而不是 wp_enqueue_scripts。 wp_head 在结束 标记之前触发。

标签: css wordpress wordpress-theming


【解决方案1】:

试试这个,

function woo_wonder_custom_style(){

 $custom_background_color = get_option('ww_background_color');

?>
<style type="text/css">
    body{
         background-color: <?php echo $custom_background_color; ?>;
    }  

<?php
}
add_action( 'wp_head', 'woo_wonder_custom_style', 9999 );
?>

希望这会对你有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-03
    • 2023-03-18
    • 2011-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-01
    • 1970-01-01
    相关资源
    最近更新 更多