我知道这是一个老问题,但更新后的答案似乎仍与给出相关。
最初的答案可能是当时最好的选择,但它没有检查帖子类型是否支持更改作者并适合元框样式。
主要PHP
add_action(\'admin_menu\', function (): void {
$type = getCurrentPostType();
if ($type) {
remove_meta_box(\'authordiv\', $type, \'normal\');
}
});
add_action(\'post_submitbox_misc_actions\', function (): void {
global $post, $user_ID;
if (!empty($post)) {
$supportsAuthor = post_type_supports($post->post_type, \'author\');
if ($supportsAuthor) {
$userId = empty($post->ID) ? $user_ID : $post->post_author;
$user = get_userdata($userId);
echo \'... fetch some template here\';
}
}
});
帮助器PHP
function getCurrentPostType(): ?string {
// https://gist.github.com/bradvin/1980309
global $post, $typenow, $current_screen;
if ($post && $post->post_type) {
return $post->post_type;
} elseif ($typenow) {
return $typenow;
} elseif ($current_screen && $current_screen->post_type) {
return $current_screen->post_type;
} elseif (isset($_REQUEST[\'post_type\'])) {
return sanitize_key($_REQUEST[\'post_type\']);
} elseif (isset($_REQUEST[\'post\'])) {
$p = get_post($_REQUEST[\'post\']);
if ($p) {
return $p->post_type;
}
}
// Unknown
return null;
}
PHTML
<div class="misc-pub-section misc-pub-author" id="author">
<?=sprintf(__(\'Author: %s\', \'aym\'), "<b>{$user->display_name}</b>")?>
<a href="#author" class="edit-author hide-if-no-js" role="button" aria-label=\'<?=__(\'Edit author\', \'aym\')?>\'>
<?=__(\'Edit\', \'aym\')?>
</a>
<div id="post-author-select" class="hide-if-js" style="display: none;">
<?php post_author_meta_box($post)?>
<p>
<a href="#author" class="save-post-author hide-if-no-js button">OK</a>
<a href="#author" class="cancel-post-author hide-if-no-js button-cancel">Cancel</a>
</p>
</div>
</div>
JavaScript
(function($) {
// Author select in admin edit sidebar
var $baseEl = $(\'#author\');
var $select = $(\'select\', $baseEl);
var $form = $(\'#post-author-select\', $baseEl);
var previousValue = $select.val();
$(\'a[href="#author"]\', $baseEl).on(\'click\', function(e) {
e.preventDefault();
$form.slideToggle(300);
});
$(\'.save-post-author\', $baseEl).on(\'click\', function(e) {
e.preventDefault();
previousValue = $select.val();
$(\'b\', $baseEl).text($("option:selected", $select).text());
$form.slideUp(300);
});
$(\'.cancel-post-author\', $baseEl).on(\'click\', function(e) {
e.preventDefault();
$select.val(previousValue);
$form.slideUp(300);
});
})(jQuery);
CSS
.misc-pub-author::before {
content: "\\f110";
color: #82878c;
font: normal 20px/1 dashicons;
speak: none;
display: inline-block;
margin-left: -1px;
padding-right: 3px;
vertical-align: top;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
#post-author-select {
padding-top: 5px;
}
#post-author-select select {
width: 100%;
}
另外,如果您遇到错误,可能是因为我使用的原始版本是在SCSS、TypeScript和PHP 7.2中制作的。并使用自定义模板引擎。