我建议您使用操作来放置元框:
function my_add_meta_box() {
    global $post;
    if ( $post && is_a( $post, \'WP_Post\' ) ) {
        $cat = get_category_by_slug(\'audio\');
        if ( !is_wp_error($cat) ) {
            if ( $post->ID == $cat->term_id ) {
               add_meta_box( \'repeatable-fields\', \'Audio Playlist\', \'repeatable_meta_box_display\', \'post\', \'normal\', \'high\');
            } else {
               # for debug only
               wp_die("Post ID {$post->ID} and Cat ID {$cat->term_id}");
            }
        } else {
           # For debug only
           wp_die(\'Cat not found by slug!\');
        }
    } else {
        # For debug only
        wp_die(\'Post not found in global space!\');
    }
}
add_action(\'admin_init\', \'my_add_meta_box\');
 编辑-从框显示中删除复选框。。。所以盒子不见了,不仅仅是空的
function repeatable_meta_box_display( $post ) {
   # Draw out your meta box fields
}
 这将改变逻辑,但效果应该是相同的,您应该停止获取未定义的索引错误。
您将收到错误,因为并非所有管理页面都有帖子id。使用操作将使代码仅在需要时工作。
如果它不起作用,而您只是想消除错误,请尝试以下方法来代替检查代码:
global $post;
if ( $post && is_a( $post, \'WP_Post\' ) ) {
   $post_id = $_GET[\'post\'] ? $_GET[\'post\'] : $_POST[\'post_ID\'] ;
   $cat = get_category_by_slug(\'audio\');
   $id = $cat->term_id;
   if ($post_id == $id) {
      # Add meta box call here
   }
}
 这应该确保该职位是你正在寻找的。
您可能需要在稍后的wordpress加载中添加第二个操作来检查post(可能它没有在admin\\u init中声明),然后如果不需要,删除meta框。
# Revised version without debug info
# Add the meta box
function my_add_meta_box() {
   add_meta_box( \'repeatable-fields\', \'Audio Playlist\', \'repeatable_meta_box_display\', \'post\', \'normal\', \'high\');
}
add_action(\'admin_init\', \'my_add_meta_box\');
# Remove the meta box if not needed once post is available
function maybe_remove_my_boxes() {
   $remove = true;
   global $post;
   if ( $post && is_a( $post, \'WP_Post\' ) ) {
      $cat = get_category_by_slug(\'audio\');
      if ( !is_wp_error($cat) ) {
          if ( $post->ID == $cat->term_id ) {
             $remove = false;
          }
      }
   }
   if ( $remove ) {
       remove_meta_box( \'repeatable-fields\', \'post\', \'normal\' );
       # echo( \'Removed as not needed\' );
   }
}
add_action(\'admin_head\', \'maybe_remove_my_boxes\');