PHP + CSS : Output Options Framework Content

标签 php css wordpress

编码 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页:

enter image description here

我的 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/

相关文章:

友好 URL 的 PHP 路由帮助

javascript - 在html中使用放大镜将鼠标悬停在图像上

php - 数组声明

javascript - 使用 PHP、AJAX 将数据发布到不同域不起作用

html - 如何在 WP 站点的表格中包含 HTML

jquery - 在 jquerymobile 中动态创建的评级星插件中未正确应用样式

php - Wordpress 短代码传递值数组

jquery - 如何使用jquery更改@media only标签内媒体标签中的CSS?

php - 为电子表格导入构建一个包含许多列的数据库表;扩展的可能性

javascript - 单击链接到页面上某个部分的菜单项时,如何隐藏汉堡包菜单?