Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

Thank you,

Nadine

share|improve this question

3 Answers 3

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.

share|improve this answer
    
Yeah I can give that a shot, I looked it over before. re: the second post, but I'm just wondering if there's a more straight-forward way to do this using just a method of "find the Category ID of this post" –  Nadine Feb 21 '12 at 23:55
up vote 0 down vote accepted

Just went a different way on this, didn't end up using categories.

share|improve this answer
2  
Could you explain why/how you made that decision, so future visitors will gain knowledge? –  GavinR Feb 27 '12 at 16:34

$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');
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.