0

「Food」というワードプレスで新しい投稿タイプを作成し、この投稿タイプに新しい投稿を追加して、サイトにユーザー登録する必要があります。

したがって、投稿タイプ「Food」に新しい投稿を追加するときは、どのユーザーに対してこの投稿を選択する必要があります。

どうやってこれを作ることができます。

ありがとう

4

1 に答える 1

0

私は現在あなたが求めていることをやっているので、あなたは今日幸運です:)

wordpress は、 の役割を持つユーザーのみを表示しますauthor。基本的に、メタ ボックスを削除authordivし、元の html と同じ html を持つ別のメタ ボックスを追加し、authordiv正しい役割/能力を持つユーザーを見つけて選択を作成する必要があるため、以下のコードは私の実装です。あなたに合わせて変更する必要がありますニーズ:

// add authordiv if user is admin or is allowed to change author of post !important
global $current_user;
if(in_array(array('administrator','change_post_author'), $current_user->allcaps))
  add_meta_box('authordiv', 'Change Author', 'my_add_author_metabox', get_post_type(), 'side');

// make new author metabox that contains users that can manage this cpt
function my_add_author_metabox()
{
  $post_type_name = isset($_GET['post_type']) ? $_GET['post_type'] : get_post_type($_GET['post']);
  $roles = get_editable_roles();
  // i unset these as they are never used
  unset(
    $roles['administrator'],
    $roles['contributor'],
    $roles['author'],
    $roles['editor'],
    $roles['subscriber']
  );

  // get role that manages this cpt
  $cpt_name = '';
  $cap = 'edit_' . $post_type_name;
  foreach($roles as $role_name => $args){
    if(array_key_exists($cap, $args['capabilities'])){
      $cpt_name = $role_name;
      break;
    }
  }

  // get users that can manage this cpt
  global $post;
  $users = get_users('role=' . $cpt_name);
  $author_id = $post->post_author;
  $select = '
<label class="screen-reader-text" for="post_author_override">Change Author</label>
<select name="post_author_override" id="post_author_override" class="">
';
  foreach($users as $user){
    if($user->ID == $author_id)
      $select .= "<option value=\"{$user->ID}\" selected=\"selected\">{$user->display_name}</option>";
    else
      $select .= "<option value=\"{$user->ID}\">{$user->display_name}</option>";
  }

  $select .= '</select>';
  echo $select;

}

現在、私のcptではサポートしていませんが、サポートしている場合は、いつからauthor削除するか、使用してくださいauthorsupportsregister_post_type()

remove_meta_box('authordiv');

うまくいけば、それはあなたのために働く、それは私のために働く. おそらくもっと良いものがあるでしょう。それを見つけたら、ここに投稿してください:)

于 2013-10-13T12:36:48.933 に答える