Multiple Meta Boxes

The previous solution for meta boxes worked fine, until I realized that I needed another one for my newsletter custom post type too. The quick solution would have been to duplicate the code and just rename and edit the necessary sections, but most of the code would remain the same. This would cause problems if it has to be edited in the future.

The same website provides a tutorial for multiple meta boxes. The required fields and other details are first stored in a $meta_boxes array, then a loop is used to go through all of them.

As explained on the tutorial, this doesn’t quite work because the callback display function can only handle one meta box at a time. The solution is to put all the functions in a class. The class constructor will initialize each meta box and hook the add and save functions onto the WordPress hooks. The website has the full explanation and sample code.

Advertisements

One thought on “Multiple Meta Boxes”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s