【问题标题】:Creating Php function to return css class创建 PHP 函数以返回 css 类
【发布时间】:2014-03-21 06:02:09
【问题描述】:

在我的 wordpress 模板中,我正在根据 wordpress 选项设置值更改 div 类。我有多个模板 /pages ,所以我不想在每个模板页面上重复 php 代码,而是想创建一个函数来返回变量 css 类($div 这里)。

Wordpress 选项设置提供 2 个变量:$pagewidth$pagecolor

PHP:

 <?php
    $pagewidth = 'big'; //big,small or tinny
    $pagecolor =  'white'; //white or black

    $divclass = array();
    $divclass = array($pagewidth,$pagecolor );
    $div = $divclass['0'].'-'.divclass['1'].'-box';
?>

css:

.big-white-box{width:100%;height:100%;background:url(images/big-white.png) repeat;}

.big-black-box{width:100%;height:100%;background:url(images/big-black.png) repeat;}

.small-white-box{width:100%;height:100%;background:url(images/small-white.png) repeat;}

.small-black-box{width:100%;height:100%;background:url(images/small-black.png) repeat;}

.tinny-white-box{width:100%;height:100%;background:url(images/tinny-white.png) repeat;}

.tinny-black-box{width:100%;height:100%;background:url(images/tinny-black.png) repeat;}

HTML:

<div class="<?php echo $div; ?>" >


<!--content-goes-here--->
</div>

帮我创建一个返回css变量$div的php函数。

【问题讨论】:

  • 这和oop没有关系...反正有什么问题?
  • 我在您的代码中看到 width:10o%; 而不是 width:100%; 请确保您在 css 中更正它
  • 感谢您在我的问题中指出我的错误,它 100% 在我的实际代码中。
  • 我有 30 多个页面,所以在每个页面中编写相同的 php 代码并不是一个好习惯。这就是为什么我想用几行代码来实现这一点。

标签: php css wordpress function oop


【解决方案1】:

没有测试它,但是像这样?

function getTemp( $WPtemplate , $pagewidth, $pagecolor ){
    $divclass = array();
    if( $WPtemplate == 1 ){
        $divclass = array($pagewidth,$pagecolor );
        $div = $divclass['0'].'-'.$divclass['1'].'-box';
        return $div;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-10
    • 1970-01-01
    相关资源
    最近更新 更多