大约一年前,我记得看到一个插件或一些代码,允许您将作者下拉菜单添加到“发布”后的元数据库中,而不是单独添加。
我似乎再也找不到这段代码了,我想知道是否有人知道如何轻松地做到这一点?
在理想情况下,我希望在Publish metabox中的“Visibility:”行下添加一行,说明“Author:”,以及当前作者的名称和名称后的编辑链接。单击编辑链接后,将显示一个下拉列表,其中包含与author metabox中可用的相同信息。
无论如何,我非常希望知道如何做到这一点。
大约一年前,我记得看到一个插件或一些代码,允许您将作者下拉菜单添加到“发布”后的元数据库中,而不是单独添加。
我似乎再也找不到这段代码了,我想知道是否有人知道如何轻松地做到这一点?
在理想情况下,我希望在Publish metabox中的“Visibility:”行下添加一行,说明“Author:”,以及当前作者的名称和名称后的编辑链接。单击编辑链接后,将显示一个下拉列表,其中包含与author metabox中可用的相同信息。
无论如何,我非常希望知道如何做到这一点。
我对管理方面的东西有点差劲,所以请仔细测试。我遇到了一些用户不在下拉列表中出现的问题,但它在默认元框中也失败了——可能是因为我的测试堆栈混乱。
add_action( \'admin_menu\', \'remove_author_box\' );
add_action( \'post_submitbox_misc_actions\', \'author_in_publish\' );
function remove_author_box() {
remove_meta_box( \'authordiv\', \'post\', \'normal\' );
}
function author_in_publish() {
global $post_ID;
$post = get_post( $post_ID );
echo \'<div class="misc-pub-section">Author: \';
post_author_meta_box( $post );
echo \'</div>\';
}
我知道这是一个老问题,但更新后的答案似乎仍与给出相关。
最初的答案可能是当时最好的选择,但它没有检查帖子类型是否支持更改作者并适合元框样式。
主要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\';
}
}
});
帮助器PHPfunction 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中制作的。并使用自定义模板引擎。或者使用插件Adminimize通过选项对此进行自定义
我在用register_taxonomy_for_object_type() 将类别分类字段添加到媒体上载(附件)。我正在使用此代码执行此操作:add_action(\'init\', \'reg_tax\'); function reg_tax() { register_taxonomy_for_object_type(\'category\', \'attachment\'); } 这可以在查看图像时为媒体页面添加一个简单的分类文本字段。我真正想要的是让它显示实