-1

我需要帮助才能从带有多维数组选项的复选框组中获取数据,以反映在我的帖子页面(single.php 代码)中。单选类型运行良好,但复选框组类型却不行。我在底部添加了在我的 single.php 中找到的用于无线电类型的示例代码,该无线电类型将数据查询到我的帖子页面以供您参考。

这是我的 metabox.php 代码中的数组:

<?php
// array
$prefix = 'wtf_';
$meta_box = array( 'id' => 'site',
               'title' => 'Program Details',
               'page' => 'post',
               'context' => 'normal',
               'priority' => 'high',
               'fields' => array(
                        array('name' => 'Principal Return',
                                'desc' => 'Principal Return After Expiry or Not',
                                'id' => $prefix . 'principal',
                                'type' => 'radio',
                                'options' => array(
                                        array('name' => '  Yes    ', 'value' => 'Yes-after expiry'),
                                        array('name' => '  No    ', 'value' => 'No-included on the interest')
                                    )
                                ),
                        array(
                        'name' => 'Compounding',
                        'desc' => 'Choose if compounding is allowed or not',
                        'id' => $prefix . 'compounding',
                        'type' => 'radio',
                        'options' => array(
                             array('name' => '  Yes    ', 'value' => 'Allowed'),
                             array('name' => '  No    ', 'value' => 'Not Allowed'),
                             array('name' => '  Re-purchase', 'value' => 'Yes thru re-purchase')
                    )
                                ),
                             array ('name' => 'Payment Processors',  
                                    'desc'  => 'Payment Processsor Accepted',  
                                    'id'    => $prefix.'processors',  
                                    'type'  => 'checkbox_group',  
                                    'options' => array(
                                                    array('label' => ' Liberty Reserve ', 'value' =>'LR'),
                                                    array('label' => ' SolidTrustPay ', 'value' =>'STP'),
                                                    array('label' => ' EgoPay ', 'value' =>'EgoPay'),
                                                    array('label' => ' Perfect Money ', 'value' =>'PM'),
                                                    array('label' => ' Payza ', 'value' =>'Payza'),
                                                    array('label' => ' PayPal ', 'value' =>'PayPal'),
                                                    array('label' => ' Bankwire ', 'value' =>'Bankwire')
                                      ))))

// Callback function to show fields in meta box
function mytheme_show_box() {
global $meta_box, $post;

// Use nonce for verification
echo '<input type="hidden" name="mytheme_meta_box_nonce" value="',    wp_create_nonce(basename(__FILE__)), '" />';

echo '<table class="form-table">';

foreach ($meta_box['fields'] as $field) {
    // get current post meta data
    $meta = get_post_meta($post->ID, $field['id'], true);

    echo '<tr>',
            '<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
            '<td>';
    switch ($field['type']) {
    case 'text':
                echo $statetemt;
            break;
        case 'textarea':
                echo $statetemt;
            break;
        case 'select':
                echo $statetemt;
            break;
        case 'radio':
            foreach ($field['options'] as $option) {
                echo $statetemt; }
            break;
        case 'checkbox':
            foreach ($field['options'] as $option) {
                echo $statetemt;}
            break;
        case 'checkbox_group':  
             foreach ($field['options'] as $option) {  
                echo '<input type="checkbox" value="'.$option['value'].'" name="'.$field['id'].'[]" id="'.$option['value'].'"',$meta && in_array($option['value'], $meta) ? ' checked="checked"' : '',' />',$option['label']; }  
                echo '<br /><span class="description">'.$field['desc'].'</span>';  
            break;
    }


//From my single.php code  <<<<=================

<div class="sdinfo"><strong>Principal Return</strong>:<span><?php $principal = get_post_meta(get_the_ID(), 'wtf_principal', true); 
      if (isset($principal[0])) { 
        echo $principal ; 
} else if (isset($principal[1])) {
        $principal = get_post_meta(get_the_ID(), 'wtf_principal', true);
        echo $principal; 
       } else {_e('Not Available');} ?></span></div>
<div class="sdinfo"><strong>Program Started</strong>:<span> <?php $started = get_post_meta(get_the_ID(), 'wtf_started', true); if (isset($started[0])) { echo $started; 
                } else {_e('Not Available');} ?></span></div>
<div class="sdinfo"><strong>Compounding</strong>:<span>
<?php $compounding = get_post_meta(get_the_ID(), 'wtf_compounding', true); 
      if (isset($compounding[0])) { 
        echo $compounding ; 
                } else if (isset($compounding[1])) {
        $compounding = get_post_meta(get_the_ID(), 'wtf_compounding', true);
        echo $compounding; 
      } else if (isset($compounding[2])) {
        $compounding = get_post_meta(get_the_ID(), 'wtf_compounding', true);
        echo $compounding; 
      } else {_e('Not Available');} ?></span></div>
?>

这给了我来自 post meta 的输出,如下所示: admin screenshot

这是我的帖子页面的输出。: 发帖页面截图

请帮忙!..我不是程序员,希望你能详细分享我的答案。提前谢谢你!

4

1 回答 1

0

您所需要的只是指向一个数组,var_dump($array)以检查您需要从数组字段中的位置和数据。

其次,您需要获得foreach()功能。例如,您想要获取一组数据:

'priority' => 'high',应该作为例子:

echo $meta_box["priority"]这将导致数组的值:high

如果您不确定并且不熟悉数组,请使用以下简单方法:

foreach ($meta_box as $arr)
{
    var_dump($arr);

    #then play and manipulate how to grab a fields a data:
    foreach ($arr["fields"][""] as $fa)
    {
        var_dump($fa["name"]);
        var_dump($fa["desc"]);
        var_dump($fa["desc"]);
    }

    #...
}

var_dump()如果您不确定数据数组,请了解如何抓取。

支付处理器 LR STP EgoP

echo $meta_box["fields"][""]["name"];

比您需要获取数组的数据:

foreach ($meta_box["fields"][""]["options"] as $options)
{
    foreach ($options as $options_key)
    {
        foreach ($options_key as $opt_val)
        {
            $result .= ' '.$opt_val;
        }
    }

}
于 2013-02-22T19:40:24.347 回答