我在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