voceconnect / multi-post-thumbnails

Adds multiple post thumbnails to a post type. If you've ever wanted more than one Featured Image on a post, this plugin is for you.
143 stars 63 forks source link

Outputting metabox by page slug or ID? #18

Closed henrywright closed 10 years ago

henrywright commented 10 years ago
new MultiPostThumbnails(
    array(
        'label' => 'Featured image 2',
        'id' => 'featured-image-2',
        'post_type' => page
    )
);

This metabox now appears on all page admin screens. Is there a way to output the metabox by page slug or by page ID? For example, I'd like the metabox to show up on my 'about-me' admin page but not on my 'contact-me' admin page.

chrisscott commented 10 years ago

You can add an action on add_meta_boxes with a lower priority than the default (10) that does the conditional check for those pages and then runs remove_meta_box() with the appropriate paramaters based on your thumbnail registration settings.

Something like this based on your code above:

add_action( 'add_meta_boxes', function() {
  if ( [check for condition you want to remove the meta box on] ) {
    remove_meta_box( 'featured-image-2-page', 'page', 'normal' );
  }
}, 20);
henrywright commented 10 years ago

@chrisscott neat solution, very much appreciated.

henrywright commented 10 years ago

I just tested the solution, it doesn't seem to work, even without the condition. I even tried various IDs such as featured-image-2-page, page-featured-image-2 and featured-image-2

Any ideas why this might not be working?

chrisscott commented 10 years ago

Sorry, I misread the registration and the remove function.. It should be: remove_meta_box( 'page-featured-image-2', 'page', 'side' );

henrywright commented 10 years ago

Ah yes, the third argument should be 'side'. All works now, thanks again.

henrywright commented 10 years ago

Here's my full function:

function my_remove_meta_boxes() {
    $screen = get_current_screen();
    if ( is_admin() && ( $screen->id == 'page' ) ) {
        global $post;
        if ( $post->ID == $id ) {
            remove_meta_box( 'page-featured-image-2', 'page', 'side' );
        }
    } 
}
add_action( 'add_meta_boxes', 'my_remove_meta_boxes', 20 );

Where $id is the ID of the page you'd like to exclude the meta box from.