Multiple Custom Post Types
In a previous article I showed you how to add the Striking Theme‘s Shortcode Generator Meta box to an individual Custom Post Type – Can you believe all the WordPress jargon in this one sentence?
If you recall it was relatively simple using the
add_meta_box()
WordPress function:
add_action('admin_menu', 'shortcode_gen_to_post_types');
function shortcode_gen_to_post_types () {
add_meta_box(
'shortcode',
__('Shortcode Generator','striking_admin'),
'theme_shortcode_generator_iframe',
'actooltip',
'normal',
'high'
);
}
The
add_meta_box
can only be used for one Custom Post Type (CPT) at a time. If you had three CPTs – ‘profiles’, ‘movies’ and ‘lunch_meats’ – you would have to create a function for each one or chain multiple
add_meta_box()'s
together in one function.
A Trick Pony will Do That
Yes, our Trick ‘PHP’ Pony can do that by grabbing the values of a multidimensional array using the PHP
foreach
statement. A multidimensional array? Maybe chaining the
add_meta_box
is an easier way to do this. You can learn a little about multidimensional arrays and the
foreach statement if you want.
Step 1: Values You will need to Have
You will need the same values as were required for the Single Custom Post.
[list type="red"]
[li] The Metabox Name – ‘shortcode’ [/li]
[li] The Admin Name of the Metabox – _(‘Shortcode Generator’, ‘striking_admin’) [/li]
[li] The Metabox Function – ‘theme_shortcode_generator_iframe‘ [/li]
[li] The Custom Post Type Slug(s) – ‘profiles’, ‘movies’, ‘lunch_meats’ We will get Yours using the get_post_type() function [/li]
[li] The Metabox Location – ‘normal’ [/li]
[li] The Priority for the Metabox – ‘high’ [/li]
[/list]
We will replace ‘profiles’, ‘movies’, ‘lunch_meats’ with your CPT slugs, by selecting for ’_builtin’ Custom Post Types equals false argument in get_post_types() .This array will get all of the Custom Post Types not created by WordPress.
Step 2: Copy and Paste
Yes, lets copy and paste the following into your function.php file – you will find this in the following WordPress directory wp-content/themes/striking/function.php.
add_action('admin_menu', 'shortcode_gen_to_post_types');
function shortcode_gen_to_post_types () {
// add_meta_box(
// 'shortcode',
// __('Shortcode Generator','striking_admin'),
// 'theme_shortcode_generator_iframe',
// '[custom post type slug]', // you can only put one value here
// 'normal',
// 'high'
// );
//
/// The page string does not accept an array we will use a foreach loop
/// to create mulitpe boxes
$buitin = array('_builtin' => false,); //Get only non built-in CPT
$post_names = get_post_types($buitin,'names'); //This will be the array of CPT names
$args = array(
'id' => 'shortcode',
'title' => __('Shortcode Generator','striking_admin'),
'pages' => $post_names, //All of the non-builtin CPT are here
'callback' => 'theme_shortcode_generator_iframe',
'context' => 'normal',
'priority' => 'high',
);
foreach ($args['pages'] as $page) {
add_meta_box($args['id'], $args['title'], $args['callback'], $page, $args['context'], $args['priority']);
}
}
Step 3: We Love Get_Custom_Post_Types
Our Trick Pony is really doing its job with the get_custom_post_types() _builtin=> false. We can sit back and watch his moves and leave our brain in our pockets and not worry if we are missing any Custom Post Slugs.
Step 4: Start Using the Shortcode Generator
Now your Trick Pony should be working and you should have those nifty Striking Shortcodes available for your Custom Post Type and you should be making those incredible unique landing pages that will increase your ROI 100 fold.
I Want to Know:
Why did KaptinLIN design the Striking Theme with the shortcode generator in a metabox instead of in WSIWG Editor as an icon?
Maybe he is saving us from an upgrade nightmare when the next version of WordPress writes a complete makeover of the Editor?