Thread Starter
slurve
(@slurve)
Anyone have an answer for this?
Try:
function admin_init(){
global $post;
if ($post->ID == '24') add_meta_box("lead_text", "Lead Text", "lead_text", "page", "normal", "high");
if (is_page_template('page_staff_bio.php')) add_meta_box("bio_excerpt", "Bio Excerpt", "bio_excerpt", "page", "normal", "high");
}
Thread Starter
slurve
(@slurve)
Thanks for the response, esmi. Doesn’t seem to work, though.
First, don’t name a function admin_init().
Second, how is this function being called? Are you calling it explicitly in a template file somewhere, or are you hooking it into a filter?
Third, what are you trying to accomplish with this function?
I assume that you’re trying to add a metabox to the post-edit screen for a specific post?
Thread Starter
slurve
(@slurve)
Chip, I’m hooking into admin_init. (Good call, I’ll change the function name.) Basically, I’m trying to add the bio_excerpt meta box to all pages using the page_staff_bio.php template.
Okay then, first things, first, here’s how you properly hook into admin_init:
function mytheme_add_bio_metabox() {
// function code goes here
}
add_action( 'admin_init', 'mytheme_add_bio_metabox' );
But actually, you don’t want to hook that function into admin_init; you can specify a much more precise action: add_meta_boxes_{post-type}.
The post-type in this case is “page”, so you should use add_meta_boxes_page:
function mytheme_add_bio_metabox() {
// function code goes here
}
add_action( 'add_meta_boxes_page', 'mytheme_add_bio_metabox' );
Next, we need to build out that function, to add the meta box. We’re going to pass the $post global variable to the function, as we’ll need it. We will determine if the current Page uses the appropriate template (as defined in the Post metadata), or if the Page has the appropriate ID. If so, we add our meta box:
function mytheme_add_bio_metabox( $post ) {
$template = get_post_meta( $post->ID, '_wp_page_template' ,true );
if ( 'page_staff_bio.php' == $template || '24' == $post->ID ) {
global $wp_meta_boxes;
add_meta_box( 'bio_excerpt', 'Bio Excerpt', 'bio_excerpt', 'page', 'normal', 'high' );
}
}
add_action( 'add_meta_boxes_page', 'mytheme_add_bio_metabox' );
Thread Starter
slurve
(@slurve)
Good to know about the add_meta_boxes_{post-type} action. Thanks. Is there a way to add the meta box only to pages using a certain template? Would I be able to use is_page_template() within mytheme_add_bio_metabox()?
Is there a way to add the meta box only to pages using a certain template?
(See my follow-up reply. 🙂 )
Thread Starter
slurve
(@slurve)
Doh. Perhaps I need an afternoon cup of coffee. 🙂 Thanks for the help, Chip.
Good idea! I think I’ll do the same. Let me know if the conditionals work as intended!
Thread Starter
slurve
(@slurve)
Worked like a champ. (Coffee and the code.) Thanks again.