0

我目前正在研究一个 PHP 类,它允许人们在 CMS 中创建新的内容块,我想要做的是让用户能够向类发送输入名称、值和以及更多选项(认为已选择,以及复选框和单选按钮的选项)

我的第一个问题是我无法将这些数据发送到我的班级,目前我正在这样做,

homepageSlider->add_meta_box(
    'Book Info',
    array(
        'Year' => array(
            'type' => 'checkbox',
            array(
                'options' => 'Year 1',
                'Year 2',
                'Year 3'
            )
        ),
        'Genre' => 'text'
    )
);

在上面你可以看到我正在创建一个名为 book info 的内容块,该内容类型有 2 个表单字段,Year(有 3 个复选框)和 Genre,它将是一个文本字段。

我的问题是这种方法似乎不起作用。我不知道如何循环遍历上述数组的每个部分以吐出我需要的所有信息?任何人都可以对此有所了解吗?

我是否正确地形成了多维数组?这是我认为需要的,还是我的班级有问题?

function() use( $box_id, $box_title, $post_type_name, $box_context, $box_priority, $fields )
{
    add_meta_box(
        $box_id,
        $box_title,
        function( $post, $data )
        {
            global $post;

            // Nonce field for some validation
            wp_nonce_field( plugin_basename( __FILE__ ), 'custom_post_type' );

            // Get all inputs from $data
            $custom_fields = $data['args'][0];

            // Get the saved values
            $meta = get_post_custom( $post->ID );

            //die(print_r($custom_fields));
            // Check the array and loop through it
            if( ! empty( $custom_fields ) )
            {

                /* Loop through $custom_fields */
                foreach( $custom_fields as $label => $type )
                {
                    $field_id_name  = strtolower( str_replace( ' ', '_', $data['id'] ) ) . '_' . strtolower( str_replace( ' ', '_', $label ) );
                    echo '<label for="' . $field_id_name . '">' . $label . '</label>';
                    //self::getFormField($type, $field_id_name);
                    //<input type="'. self:getFormField($type) . '" name="custom_meta[' . $field_id_name . ']" id="' . $field_id_name . '" value="' . $meta[$field_id_name][0] . '" />';
                }
            }

        },
        $post_type_name,
        $box_context,
        $box_priority,
        array( $fields )
    );
}
4

1 回答 1

0

我认为你正在形成错误的数组。

homepageSlider->add_meta_box(   
    'Book Info',
    array(  
        'Year' => array('type' => 'checkbox' , 'options' => array('Year 1', 'Year 2', 'Year 3')),  
        'Genre' => array('type' => 'text'),
    )  
);

但它看起来很乱。我相信用这样的数组来做这不是一种直观的方式。您是否考虑过将其抽象为类?我认为这样的事情会更干净:

class metaBox {
     private $inputFields = array();
     public function addField($inputFieldObject) {
       $this->inputFields[] = $inputFieldObject;
     }
}

class inputField {
     public $type = "";  // which tags
     public $attributes; // which attributes
}
于 2013-02-14T10:12:25.740 回答