我正在关注 this创建自定义 Wordpress 选项的教程
theme/functions/admin-menu.php 中改变背景颜色的函数:
// Color Scheme
function color_scheme_setting() {
$options = get_option('plugin_options');
$items = array("Red", "Green", "Blue");
echo "<select name='plugin_options[color_scheme]'>";
foreach ($items as $item) {
$selected = ( $options['color_scheme'] === $item ) ? 'selected = "selected"' : '';
echo "<option value='$item' $selected>$item</option>";
}
echo "</select>";
}
header.php:
<style>
body {
background: <?php echo $options['color_scheme']; ?>
}
</style>
</head>
<body <?php body_class(); ?>>
<div id="wrapper">
<div id="header">
<h1>
<a href="<?php echo home_url( '/' ); ?>" title="<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>" rel="home"><?php bloginfo( 'name' ); ?></a>
</h1>
<?php $options = get_option('plugin_options'); ?>
<h2> <?php echo $options['banner_heading']; ?> </h2>
<img src="<?php echo $options['logo']; ?>" alt='' />
<p><?php echo $options['color_scheme']; ?></p>
<div id="lang">
<?php do_action('icl_language_selector'); ?>
<?php _e( 'english', 'starkers' ); ?>
</div>
<ul id="nav">
<?php wp_list_pages('title_li='); ?>
</ul>
当我在仪表板中选择“绿色”时,$options['color_scheme']
在背景选择器中没有出现。
(但它确实出现在 $options['color_scheme']
内的 <p>
标签中
有什么建议可以让这个数组在后台选择器中工作吗?
(我使用的是 Wordpress 3.03)
编辑:
我刚试过这个:
<p style="color: <?php echo $options['color_scheme']; ?>"><?php echo $options['color_scheme']; ?></p>
和 <p>
标签改变了颜色
为什么它在 <style>
之间不起作用标签?
最佳答案
$options
变量仅在您的 <h1>
之后初始化元素被渲染。当您想在 <style>
中使用它时,它的值尚不可用 block 。
您可能希望更早地初始化该变量:
<?php $options = get_option('plugin_options'); ?>
<style>
body {
background: <?php echo $options['color_scheme']; ?>
}
</style>
关于css - PHP 数组在 HTML 标签中显示值但在 CSS 选择器(WordPress)中不显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4481986/