[英]Wordpress Author box Control Function
您好朋友,我在Wordpress中几乎不需要帮助。 我试图隐藏仅在特定用户的帖子下方显示的authorbox。
例如,如果我正在查看的帖子是由admin发布的,那么我想将authorbox隐藏在admin发布的帖子内容下,但应该为所有其他用户显示? 我尝试了不同的功能,但未能成功。
我想完全隐藏authorbox,我知道可以用CSS代码完成
.author-box { display:none;}
但这隐藏了完成它们的整体authorbox,我只想在admin发表的帖子上隐藏authorbox?
我正在使用创世纪框架,所以请提出建议,如果您可以在这里提供帮助。
谢谢
在主题的functions.php
文件中,添加以下内容:
function remove_my_post_metabox() {
global $post;
// If the post author ID is 1, then remove the meta box
// You would need to find the ID of the author, then put it in place of the 1
if ( $post->post_author == 1) {
remove_meta_box( 'authordiv','post','normal' ); // Author Metabox
}
}
add_action( 'add_meta_boxes', 'remove_my_post_metabox' );
如果您需要找出作者的角色,并根据角色(例如admin)真正地做到这一点,那么它将更像是这样:
function remove_my_post_metabox() {
global $post;
// If there's no author for the post, get out!
if ( ! $post->post_author) {
return;
}
// Load the user
$user = new WP_User( $post->post_author );
// Set "admin" flag
$is_admin = FALSE;
// Check the user roles
if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
foreach ( $user->roles as $role ) {
if ( $role == 'administrator' ) {
$is_admin = TRUE;
}
}
}
// Lastly, if they ARE an admin, remove the metabox
if ( $is_admin ) {
remove_meta_box( 'authordiv','post','normal' ); // Author Metabox
}
}
add_action( 'add_meta_boxes', 'remove_my_post_metabox' );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.