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.

In my plugin I want to display an add_meta_box under the WYSISWYG in the new post page, which is no problem and works fine. But the box should only appear when a specific category (example: category with id = 5) is selected, otherwise it is not visible.

How can this be achieved?

Edit as requested in the comments here is my jquery:

$script = "
            <script type='text/javascript'>
                jQuery(document).ready(function($) {
                    $('#metabox_sectionid').hide();
                    $('#in-category-".CATEGORY."').is(':checked') ? $('#metabox_sectionid').show() : $('#emetabox_sectionid').hide();
                    $('#in-category-".CATEGORY."').click(function() {
                        $('#metabox_sectionid').toggle(this.checked);
                    });
                });
            </script>";
        echo $script;

BR & Thanks, mybecks

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

You could try this: get the ID's of the attached categories and wrap your add_meta_box function inside a simple test against your defined cat ID.

add_action('add_meta_boxes', 'is_in_cat_example');

function is_in_cat_example() {
    global $post;
    $category = '5'; // can be an array of ID's as well

    if ( in_category( $category, $post ) ) {
    add_meta_box(...);
        }

}
share|improve this answer
    
An additional note: If you want it to appear on selection, you will need to do that via jQuery, the way this answer stands, it will take a save then a page reload to get the box to appear. Good answer though, +1 –  m0r7if3r Jan 31 '12 at 11:16
    
Thanks, forget to mention that. –  ungestaltbar Jan 31 '12 at 11:19
    
Thanks that is exactly what I need. Now adding the jQuery part, to make it smoother. :) –  mybecks Jan 31 '12 at 13:57
    
cool, would be nice if you can post your solution for the jquery part. –  ungestaltbar Jan 31 '12 at 14:21
    
@ungestaltbar i've updated my question with the jquery part. hope that helps. –  mybecks Mar 8 '13 at 7:40
add comment

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.