I am a WP beginner and see the potential of the “Meta Box” plugin (http://wordpress.org/plugins/meta-box/). I have already activated the plugin. So far I managed to upload the demo.php and to display the various meta boxes in the blog post window.
I have already read the documentation, but still I don’t know how to register the meta boxes for my custom post type. How do I implement them?
The code of my custom post type (it’s about listing artists) looks like this so far:
add_action('init', 'add_cpt_artists');
function add_cpt_artists() {
$labels = array(
'name' => _x('Artists', 'post type general name'),
'singular_name' => _x('Artist', 'post type singular name'),
'add_new' => _x('Add', 'artist'),
'add_new_item' => __('Add new artist'),
'edit_item' => __('Edit artist'),
'new_item' => __('New artist'),
'view_item' => __('View artist'),
'search_items' => __('Search for artist'),
'not_found' => __('No artist found'),
'not_found_in_trash' =>
__('No artist in trash'),
'parent_item_colon' => ''
);
$supports = array('title', 'editor', 'thumbnail', 'excerpt');
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'_builtin' => false,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array("slug" => "artists"),
'capability_type' => 'post',
'hierarchical' => false,
'has_archive' => true,
'hierarchical' => false,
'menu_position' => 10,
'supports' => $supports
);
register_post_type('artists',$args);
}
Let’s say from the Meta Box plugin I want to add two text areas and a box with checkboxes.
- Where would I insert the code?
- What would the code look like?
- How can I retrieve the content in my page template?
In this instance you would need to find the hook into the specific metabox and add your custom post type to its
pages
array, for example:The Meta Box plugin essentially adds a class that makes it easier to code metaboxes. You still need to actually register and code the metaboxes themselves into your theme’s files (see example below). To display only on your custom post type you need to modify the
pages
array (to add'artists'
to that array as shown above).Code example (not tested)
Assuming you’ve got the plug in installed and activated, you should be able to add some text areas and checkbox meta boxes by pasting the following code to your themes
functions.php
file:Then to display the options on the front-end in your theme files you can use the helper function, e.g.
These examples are modified from the plugin’s Github repo where the demo file (linked) contains code on using a variety of metabox types, not just textareas and checkboxes.
This tutorial might be helpful.
You can then retrieve the data for your theme using
get_post_meta()