Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php WordPress-“作者”下拉菜单不记得作者是谁_Php_Wordpress - Fatal编程技术网

Php WordPress-“作者”下拉菜单不记得作者是谁

Php WordPress-“作者”下拉菜单不记得作者是谁,php,wordpress,Php,Wordpress,我添加了一个过滤器,使普通用户可以被选为文章的作者。在选择作者并更新帖子后,用户将被分配给该帖子,该功能运行良好 问题是,这样做之后-当你转到文章的前端,然后回到后端,然后作者下拉菜单切换回管理员,好像WordPress记不起它被设置为什么,所以如果你更新文章,它将再次被分配给管理员 wordpress似乎忘记了谁被选为作者。我如何修改过滤器,使下拉菜单记住谁被选为一篇文章的作者 add_filter('wp_dropdown_users', 'theme_post_author_overrid

我添加了一个过滤器,使普通用户可以被选为文章的作者。在选择作者并更新帖子后,用户将被分配给该帖子,该功能运行良好

问题是,这样做之后-当你转到文章的前端,然后回到后端,然后作者下拉菜单切换回管理员,好像WordPress记不起它被设置为什么,所以如果你更新文章,它将再次被分配给管理员

wordpress似乎忘记了谁被选为作者。我如何修改过滤器,使下拉菜单记住谁被选为一篇文章的作者

add_filter('wp_dropdown_users', 'theme_post_author_override');
function theme_post_author_override($output)
{
  // return if this isn't the theme author override dropdown
  if (!preg_match('/post_author_override/', $output)) return $output;

  // return if we've already replaced the list (end recursion)
  if (preg_match ('/post_author_override_replaced/', $output)) return $output;

  // replacement call to wp_dropdown_users
 $output = wp_dropdown_users(array(
   'echo' => 0,
  'name' => 'post_author_override_replaced',
  'selected' => empty($post->ID) ? $user_ID : $post->post_author,
  'include_selected' => true
 ));

 // put the original name back
 $output = preg_replace('/post_author_override_replaced/', 'post_author_override', $output);

  return $output;
}

这可能已经解决了,但我只是有同样的问题,并解决了它,所以想分享解决方案。上面的代码需要在第4行中添加一行。 我希望它能帮助别人

add_filter('wp_dropdown_users', 'theme_post_author_override');
function theme_post_author_override($output)
{
global $post, $user_ID; // <-- You need this line
// return if this isn't the theme author override dropdown
if (!preg_match('/post_author_override/', $output)) return $output;

// return if we've already replaced the list (end recursion)
if (preg_match ('/post_author_override_replaced/', $output)) return $output;

// replacement call to wp_dropdown_users
$output = wp_dropdown_users(array(
    'echo' => 0,
    'name' => 'post_author_override_replaced',
    'selected' => empty($post->ID) ? $user_ID : $post->post_author,
    'include_selected' => true
));


// put the original name back
$output = preg_replace('/post_author_override_replaced/',         'post_author_override', $output);

return $output;
}

感觉有点像你在试图重新发明轮子,检查一下共同作者:你是对的,我用了这个插件。谢谢你,如果你想发布它作为答案,那么我会选择它作为正确的。