在过去的几天里,我一直在做一个Wordpress主题项目,我一直在思考如何使用OOP让动态选项页面正常运行(我主要是一个主题开发人员,而不是PHP脚本编写人员)。
<?php
$footerpage = new optionpage;
$footerpage->title = 'Footer';
$footerpage->titleprint = ' Footer Options';
$footerpage->slug = 'footer';
$footerpage->html = array(
'1' => array(
'type' => 'textarea',
'class' => 'large-text',
'name' => 'html',
'title' => 'HTML',
'description' => 'Type in whatever HTML you\'d like to see in the footer here:',
),
'2' => array(
'type' => 'input',
'class' => 'large-text',
'name' => 'background-color',
'title' => 'Background Color',
'description' => ' Choose a Background Color:'
),
);
class optionpage {
public $title;
public $titleprint;
public $slug;
public $html = array();
......
......
......
public function ab_settings() {
register_setting( $this->slug, 'ab_options');
add_settings_section('ab_section', '', array(&$this, 'ab_do_titleprint'), 'ab_' . $this->slug . '_options', 'ab_options' );
foreach ($this->html as $key => $html) {
add_settings_field( $key, $html['title'], array(&$this, 'ab_do_htmlprint' ), 'ab_' . $this->slug . '_options', 'ab_section');
}
}
public function ab_do_htmlprint() {
$html = $this->html[$key];
?>
<p><?php echo $html['description'] ?></p>
<<?php echo $html['type'] ?>
id="<?php echo $html['id'] ?>"
class="<?php echo $html['class'] ?>"
name="<?php echo $html['name'] ?>">
<?php get_option ($html['name'])?>
</<?php echo $html['type'] ?>>
<?php
}
......
......
?>在这个代码示例中,我试图让函数ab_do_htmlprint识别它被调用的地方的foreach表达式,因为该函数将在foreach循环中被调用任意多次。
我尝试了几种方法,比如在函数名后附加一个变量,但这需要使用相同代码的多个函数,只是名称不同而已。我也尝试过通过引用传递各种变量,但是它们也不起作用,如果需要的话,我可能没有正确地这样做。
不管怎么说,要高效地完成这个任务呢?
发布于 2010-11-27 05:55:08
如果我理解正确的话,您需要将一个参数传递给ab_do_htmlprint。add_settings_field有此参数,请执行以下操作:
add_settings_field( $key, $html['title'], array(&$this, 'ab_do_htmlprint' ), 'ab_' . $this->slug . '_options', 'ab_section', array($key));然后:
public function ab_do_htmlprint($key) {
$html = $this->html[$key];等。
发布于 2010-11-27 06:27:48
如果我理解正确的话,你有一个数组的值,你想要显示在一个组的管理屏幕上的选项。
也许最短的例子是我在这里发布的:http://swiftthemes.com/forums/showthread.php?383-SWIFT-on-a-WordPRess-MU-install/page2
附注:在http://wordpress.stackexchange.com上发布WordPress问题:更多WordPress专家!
发布于 2010-11-27 05:52:01
我不熟悉Wordpress api,所以请验证我的一些假设。(我知道这不是一个答案,但我不想将其作为可读性的注释)
1) ab_settings()在什么地方被调用?
2)您遇到问题的部分是什么?
foreach ($this->html as $key => $html) {
add_settings_field( $key, $html['title'], array(&$this, 'ab_do_htmlprint' ),
'ab_' . $this->slug . '_options', 'ab_section');
}3) $key是魔术般的可用的,还是你需要提供它作为参数?
public function ab_do_htmlprint() {
$html = $this->html[$key];`此外,您的ab_do_htmlprint函数的格式非常难以阅读,并且可能是错误的来源。尝试使用HEREDOC表示法或纯PHP字符串
例如)而不是
public function ab_do_htmlprint() {
$html = $this->html[$key];
?>
<p><?php echo $html['description'] ?></p>
<<?php echo $html['type'] ?>
id="<?php echo $html['id'] ?>"
class="<?php echo $html['class'] ?>"
name="<?php echo $html['name'] ?>">
<?php get_option($html['name'])?>
</<?php echo $html['type'] ?>>
<?php
}作为
public function ab_do_htmlprint() {
$html = $this->html[$key];
$output = "<p>{$html['description']}</p>";
$output .= "<{$html['type']} id=\"{$html['id']}\"";
$output .= " class=\"{$html['class']}\"";
$output .= " name=\"{$html['name']}\">";
get_option( $html['name'] );
$output .= "</{$html['type']}>";
echo $output;
}https://stackoverflow.com/questions/4288666
复制相似问题