2

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

2

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.

1
  • 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
0

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

1
  • 2
    Could you explain why/how you made that decision, so future visitors will gain knowledge?
    – GavinR
    Feb 27 '12 at 16:34
0

$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');
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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