如何允许用户更改窗口小部件标题颜色?

时间:2013-09-29 16:22:36

标签: php wordpress function

所以我正在构建这个主题,我希望允许用户更改小部件标题背景的颜色。因此在functions.php中,我注册了侧边栏,我得到了:

      'before_widget' => '',
      'after_widget' => '',
      'before_title' => '<div class="widget-header"><p>',
      'after_title' => '</p></div>',

为了让人们改变标题背景的颜色我添加了:

'before title' => '<div class="widget-header" style="background: #ccc;"><p>'

这仍然有效。我创建了一个主题选项页面,人们可以在其中选择颜色,然后将代码更改为:

'before title' => '<div class="widget-header" style="background: <?php echo get_option('shoboto_maincolor'); ?>;"><p>'

现在,当我保存时,页面变为空白。我完全肯定的是style =“background :;”是正确的,因为它改变了网站标识的颜色,其代码位于header.php中。所以我认为它与加载wordpress元素的顺序有关。我对吗?我的意思是标题(我认为)在我的选项页面传递值后加载,但是functions.php加载更早。我对吗?如果是这样,我该如何解决这个问题呢?您知道允许用户更改颜色的任何其他方式吗?

1 个答案:

答案 0 :(得分:1)

如果你看一下

   'before title' => '<div class="widget-header" style="background: <?php echo get_option('shoboto_maincolor'); ?>;"><p>'

你会注意到你的PHP代码的其余部分中有<?php ?>

所以最终结果看起来像是

<?php .... <?php ... ?> ... ?>

这将输出一个错误,因为默认情况下打开调试,你会得到白页,因为文档无法解析......

因此您需要将该行更改为

'before title' => '<div class="widget-header" style="background:' . get_option('shoboto_maincolor') . '"><p>',

为了摆脱解析错误