带有复选框的Metabox不起作用!

时间:2018-12-07 作者:sh.dehnavi

请帮帮我。我想在元框中显示用户的显示名称作为复选框。然后,我可以通过以下方式选择许多用户在帖子中显示一些内容:

if (in_array(\'special userID\', get_post_meta($post->ID, \'U_S_C_users\', true)) == true) {
  // Show the content here
  echo "ok";
}
根据我的代码,我看到以下错误:

warning: in_array() expects parameter 2 to be array in string:::: if (in_array($user->ID,$savedusers)){
我的代码:

<?php

/* Define the custom box */
add_action(\'add_meta_boxes\', \'User_specific_content_box\');

/* Adds a box to the main column on the custom post type edit screens */
function User_specific_content_box() {
    add_meta_box(\'User_specific_content\', __( \'User specific content box\'),\'User_specific_content_box_inner\',\'class\');
}

/* Prints the box content */
function User_specific_content_box_inner() {
    global $post,$wp_roles;
    $savedusers = get_post_meta($post->ID, \'U_S_C_users\',true);
    var_dump($savedusers);

    // Use nonce for verification
    wp_nonce_field( plugin_basename(__FILE__), \'User_specific_content_box_inner\' );
    echo \'<h4>\'.__(\'By User Name:\').\'</h4>\';
    $blogusers = get_users(\'blog_id=1&orderby=nicename\');
    $usercount = 0;
    foreach ($blogusers as $user) {
        echo \'<input type="checkbox" name="U_S_C_users" value="\'.$user->ID.\'"\';
        if (in_array($user->ID,$savedusers)){
            echo \' checked\';
        }
        echo \'>\'.$user->display_name.\'    \';
        $usercount = $usercount + 1;
        if ($usercount > 5){
            echo \'<br/>\';
            $usercount = 0;
        }
    }
}


/* Save Meta Box */
add_action(\'save_post\', \'User_specific_content_box_inner_save\');

/* When the post is saved, saves our custom data */
function User_specific_content_box_inner_save( $post_id ) {
    global $post;
      // verify this came from the our screen and with proper authorization,
      // because save_post can be triggered at other times

      if ( !wp_verify_nonce( $_POST[\'User_specific_content_box_inner\'], plugin_basename(__FILE__) ) )
          return $post_id;

      // verify if this is an auto save routine. 
      // If it is our form has not been submitted, so we dont want to do anything
      if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE ) 
          return $post_id;
      // OK, we\'re authenticated: we need to find and save the data

    $savedusers = get_post_meta($post_id, \'U_S_C_users\',true);

    if (isset($_POST[\'U_S_C_users\']) && !empty($_POST[\'U_S_C_users\'])){
        foreach ($_POST[\'U_S_C_users\'] as $u){
            $new_users[] = $u;
        }
        update_post_meta($post_id, \'U_S_C_users\', $new_users);
    }else{
        if (count($savedusers) > 0){
            delete_post_meta($post_id, \'U_S_C_users\');
        }
    }
}
?>
我的问题:

1- i see that error
2- i want to show just users with author role.
谢谢你的帮助

1 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

发生该错误的原因是$savedusers 不是数组&mdash;in_array() 要求second参数是有效数组,如果不是,则引发错误(或警告)。

一个简单的解决方法是将变量类型转换为如下数组:

$savedusers = (array) get_post_meta( $post_id, \'U_S_C_users\', true );
或者更好,使用wp_parse_id_list() 这确保了$savedusers 是(用户)ID的数组:

$savedusers = wp_parse_id_list( get_post_meta( $post->ID, \'U_S_C_users\', true ) );
(您将在User_specific_content_box_inner()User_specific_content_box_inner_save() 哪里get_post_meta() 已调用。)

其次,复选框应通过附加[] 给他们name 像这样:

echo \'<input type="checkbox" name="U_S_C_users[]" value="\'.$user->ID.\'"\';

相关推荐

保存Metabox内容无效

我正在尝试保存一个metabox输入,但它似乎不起作用。我使用的是数组(因为我需要我的metabox有60行),所以我假设问题就出在数组中。这是我为管理员提供的metabox函数(它可以正确显示我要显示的信息):function mock_metabox() { global $post; // Nonce field wp_nonce_field( basename( __FILE__ ), \'mock_fields\' ); // in