编码 ub3r n00b 的两个问题...
首先,我正在使用 Devin Price 的 Options Framework Theme 并且只是想知道如何在 <head>
中输出背景属性。我的部分 header.php
文件,从主题选项页面仅当用户上传了图片或为此选择了颜色?
我的 options.php
:
$options[] = array(
'name' => __('Background', 'options_framework_theme'),
'desc' => __('Add a background.', 'options_framework_theme'),
'id' => 'background-one',
'std' => $background_defaults,
'type' => 'background' );
我的 Theme Options
页:
我的 header.php
:
<style type="text/css">
<?php $background = of_get_option('background-one');
echo 'body {';
if ($background['color']) {
echo '
background: ' .$background['color']. ';';
}
if ($background['image']) {
echo '
background: url('.$background['image']. ') ';
echo ''.$background['repeat']. ' ';
echo ''.$background['position']. ' ';
echo ''.$background['attachment']. ';';
}
echo '
}';
?>
</style>
这在我网站的前端工作得很好,将 CSS 显示为:
body {
background: #8224e3;
background: url(images/bg03.gif) repeat top left fixed;
}
但是如果用户没有通过主题选项页面选择颜色或图像作为背景,源代码将输出:
body {
}
如果用户没有选择背景,我如何才能删除上面的 CSS?
据我所知,if
需要创建语句,但我不知道如何正确编写它,因为我对 php 还很陌生。
其次,如何在框架中设置默认背景图片?
我的 options.php
:
// Background Defaults
$background_defaults = array(
'color' => '',
'image' => '',
'repeat' => 'no-repeat',
'position' => 'top left',
'attachment' => 'fixed' );
谢谢
最佳答案
只需将一些内容移动到 if 语句中,如下所示:
<?php
$background = of_get_option('background-one');
if ($background['color'] || $background['image']) {
echo '<style type="text/css" >';
echo 'body {';
if ($background['color']) {
echo '
background: ' .$background['color']. ';';
}
if ($background['image']) {
echo '
background: url('.$background['image']. ') ';
echo ''.$background['repeat']. ' ';
echo ''.$background['position']. ' ';
echo ''.$background['attachment']. ';';
}
echo '
}';
echo '</style>';
}
?>
并且,对于您的第二个问题,只需进行以下更改:
// Set up a default image
// NOTE: This is designed for the image to be located in your theme folder, inside an images folder
$default = get_bloginfo("template_url") . 'images/default.jpg';
// Background Defaults
$background_defaults = array(
'color' => '',
'image' => $default,
'repeat' => 'no-repeat',
'position' => 'top left',
'attachment' => 'fixed' );
关于PHP + CSS : Output Options Framework Content,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13906286/