1

WordPress で複数のメタ ボックスを作成しています。ユーザーが特定のボックスのオンとオフを切り替えられるようにしたいと考えています。

そのため、ラジオ ボタン (別のメタ ボックス内) をクリックしてオプションを更新すると、そのラジオ値に関連付けられた追加のメタ ボックスが表示されます。

したがって、基本的には、1 つのメタ ボックス (セレクター ボックス) から 2 つのメタ ボックス (セレクター ボックスと、選択したばかりの新しいボックス) になります。

私のコードでは、次のような設定を行うことでメタ ボックスを作成できます (これは、ユーザーが他のメタ ボックスのオン/オフを切り替えるために選択するラジオ ボックスです)。

// 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 では次のように表示されます。

WordPress のボックスタイトル

上記のメタ ボックスで選択すると (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',

私の計画は、['lead'] 値 (上記のメタ ボックス コードから) を ['value'] 値 (ラジオ メタ ボックスから) と一致させて、接続できるようにし、IF でテストすることでした。ステートメントを使用して、それらが同じものであることを確認し、両方が value_one と等しい場合にのみ表示します。

以下の関数は、実際に WordPress にメタ ボックスを追加するものです。その関数内で、これら 2 つを一致させるために、次の IF ステートメントを作成してみました。

if($this->_meta_box['value'] == $this->_meta_box['lead'])

['value'] が複数の配列内にネストされているため (または問題だと思います)、それをターゲットにする方法がわかりません。

完全な機能は次のとおりです。

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']);
        }
    }
}
4

1 に答える 1

0

PHPで多次元配列値にアクセスするには、次の構文を使用します。

$arrayVar['key1']['key2'];

したがって、最初の配列の「value」キーにアクセスするには、次のようにします。

$this->_meta_box['fields']['options'];
/* returns 
array(
  array('name' => 'Value 1', 'value' => 'value_one'),
  array('name' => 'Value 2', 'value' => 'value_two'),
);
*/
// to get the first option's value
$this->_meta_box['fields']['options'][0]['value'];
// to get the second option's value
$this->_meta_box['fields']['options'][1]['value'];
于 2012-03-28T15:30:30.103 に答える