【发布时间】: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