我在WordPress创建多个元盒,我希望用户能够打开或关闭某些框。如何访问多维数组中的'值'键?
因此,他们点击一个单选按钮(在单独的元框中),更新选项,并且会出现附加到该无线电值的附加元框。因此,它基本上从一个元框(选择框)到两个元框(选择框,以及他们刚刚选择的新框)。
在我的代码,你可以通过设置这样的事情了创建一个元包(这是用户选择其他元盒可开启/关闭无线电盒):
// Create meta box that gives user ability to select additional meta box,
// which will then show upon saving the post
$meta_boxes[] = array(
'id' => 'meta_box_id',
'title' => 'Box title',
'pages' => array('page'),
'context' => 'side',
'priority' => 'low',
'fields' => array(
array(
'name' => 'Select:',
'id' => $prefix . 'meta_box_id',
'type' => 'radio',
'options' => array(
array('name' => 'Value 1', 'value' => 'value_one'),
array('name' => 'Value 2', 'value' => 'value_two'),
)
)
)
);
这里它看起来像在WordPress:
这里的是,在上面的meta框一旦选定另一元框(假设value_one被选中),将出现在后屏幕上:
// This meta box will only show if 'value_one' is selected from
the radio box above
$meta_boxes[] = array(
'id' => 'standard_lead',
'title' => 'Standard Lead',
'pages' => array('page'),
'context' => 'normal',
'priority' => 'high',
'lead' => 'value_one',
'fields' => array(
array(
'type' => 'text',
'id' => $prefix . 'standard_title',
'name' => 'Lead Title',
'desc' => 'The title of your Standard Lead',
'width' => '100'
),
array(
'type' => 'textarea',
'id' => $prefix . 'standard_content',
'name' => 'Lead Content',
'desc' => 'The content of your Standard Lead (you can use HTML)',
'width' => '100'
)
)
);
从代码的重要的部分是这样的:
'lead' => 'value_one',
我的计划是具有[“引”]的值(从正上方的meta框代码)以匹配[“值”]值(来自无线电元框),因此它们可以连接,然后使用IF语句进行测试,以确保它们相同,然后只在它们都等于value_one时才显示。
下面的函数实际上是将元框添加到WordPress中。在这一功能,我试图创建这个IF语句这两个搭配在一起:
if($this->_meta_box['value'] == $this->_meta_box['lead'])
,但它不工作,我不知道该如何定位[“值”],因为它是嵌套在多个阵列内的(或者我认为,是问题)。
下面是完整的功能:
function add_meta_boxes()
{
$this->_meta_box['context'] = empty($this->_meta_box['context']) ? 'normal' : $this->_meta_box['context'];
$this->_meta_box['priority'] = empty($this->_meta_box['priority']) ? 'high' : $this->_meta_box['priority'];
foreach($this->_meta_box['pages'] as $page)
{
if($this->_meta_box['value'] == $this->_meta_box['lead'])
{
// adds meta box to WP
add_meta_box($this->_meta_box['id'], $this->_meta_box['title'], array(&$this, 'show_meta_boxes'), $page, $this->_meta_box['context'], $this->_meta_box['priority']);
}
}
}
你在哪里得到'$ this - > _ meta_box'?该数组的内容是什么? – jeremyharris 2012-03-28 15:12:26
@jeremyharris我制成一个foreach“的foreach($ meta_boxes为$ meta_box)”即取“meta_boxes []”的每个实例,并用它来从它拉值。 – 2012-03-28 15:23:43