Limit a meta box to a specific category

I’m trying to limit a meta_box to posts in a specific category. I’m using this method: http://www.farinspace.com/page-specific-wordpress-meta-box/ and it works rather well But I can’t seem to get it to work with categories. Heres’ my code:

<?php 
// meta box for news category
add_action('admin_init','news_init');

function news_init(){

    $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;

    $post = get_post($post_id);

    if ($post->post_category == '6'){
    add_meta_box('news_details', 'News Details', 'news_details', 'post');
    }
}
?>

I feel like I’m missing a step. Thoughts or ideas?

Read More

Thank you,

Nadine

Related posts

Leave a Reply

3 comments

  1. You might want to check out his other post: http://www.farinspace.com/show-hide-meta-box-by-category/

    I figure you would need some javascript because if you change categories, you would need some way for the browser to trigger to show the metabox, hence javascript.

    He has a video tutorial, so that should help explain everything. Also, since you are already reading his blog, you might want to try out his WPAlchemy_MetaBox Class. I use it every time in my themes and I love it.

  2. $post->post_category is returning an Array
    So you shoul tes like this

    if ($post->post_category[0] == '6'){
       add_meta_box('news_details', 'News Details', 'news_details', 'post');
    }