0

The auestion is, why my foreach loop renders metabox only with information from last array? The code of class:

class CusomMetaBox {

/**
 * Hook into the appropriate actions when the class is constructed.
 */
//public function __construct( $post_type, $unicname, $headline, $fieldDisc ) {
public function __construct( $array ) {

global $post;

foreach ( $array as $metabox ) {

  $this->post_type = $metabox['post_type'];
  $this->unicname = $metabox['unicname'];
  $this->headline = $metabox['headline'];
  $this->fieldDisc = $metabox['field_desc'];

add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) );
add_action( 'save_post', array( $this, 'save' ), 1, 2);
}
}
here a goes other methods that render and save metaboxes...
public function add_meta_box() {

        add_meta_box(
            $this->unicname
            ,$this->headline
            ,array( $this, 'render_meta_box_content' )
            ,$this->post_type
            ,'advanced'
            ,'high'
        );

}

This is function from functions.php that initialize the class:

function call_createCustomMetaBox() {
new CusomMetaBox( array(
'eventdata'=>array(
  'post_type'=>'event',
  'unicname'=>'eventdata',
  'headline'=>'Event Date',
  'field_desc'=>'Enter the date of your event'
),
'eventplace'=>array(
  'post_type'=>'event',
  'unicname'=>'eventplace',
  'headline'=>'Place of the event',
  'field_desc'=>'Enter address of the event'
),
'productcost'=>array(
  'post_type'=>'product',
  'unicname'=>'productcost',
  'headline'=>'Cost of the product',
  'field_desc'=>'Enter the cost of the product. For example: 50.00$ per hour'
)
) );
}

I cant figrue out, why i got only 1 meta box instead of 3 (2 in event post and 1 in product post) ?

0

1 Answer 1

0

Its because you are assigning your last array element to your class properties post_type,unicname,headline,fieldDisc

Since the input is an array you are overriding the class properties again and again, only last array element is saved there(post_type,unicname,headline,fieldDisc).

So what I would suggest is either store the whole array in one property or make each property an array so that they get room for holding multiple values

public function __construct( $array ) { 
global $post;
$this->metabox_array = $array;   
add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) );
add_action( 'save_post', array( $this, 'save' ), 1, 2);    
}

//here a goes other methods that render and save metaboxes...
public function add_meta_box() {
   //looping over all input array
foreach ( $this->metabox_array as $metabox ) {

        add_meta_box(
            $metabox['unicname']
            ,$metabox['headline']
            ,array( $this, 'render_meta_box_content' )
            ,$metabox['post_type']
            ,'advanced'
            ,'high'
        );

   }

}

Your render_meta_box_content should have content accordingly, by looping over the array.

Not the answer you're looking for? Browse other questions tagged or ask your own question.