WordPress功能函数author_can()

WordPress功能函数author_can(),返回所提供文章的作者是否具有指定的能力。

用法:

author_can( int|WP_Post $post, string $capability, mixed $args )

描述

这个函数还接受一个对象的ID,以检查该功能是否是元功能。诸如edit_post和edit_user之类的元功能是map_meta_cap()函数用来映射用户或角色具有的原始功能的功能,如edit_posts和edit_others_posts。

使用示例:

author_can( $post, ‘edit_posts’ );

author_can( $post, ‘edit_post’, $post->ID );

author_can( $post, ‘edit_post_meta’, $post->ID, $meta_key );

参数:

$post

(int|WP_Post) (必需) Post ID或Post对象。

$capability

(string) (必需) 功能名称。

$args

(mixed) (可选) 其他参数,通常从对象ID开始。

返回:

(bool) 文章作者是否具有所赋予的能力。

来源

文件: wp-includes/capabilities.php

function author_can( $post, $capability, …$args ) {

$post = get_post( $post );

if ( ! $post ) {

return false;

}

$author = get_userdata( $post->post_author );

if ( ! $author ) {

return false;

}

return $author->has_cap( $capability, …$args );

}

更新日志:
WordPress功能函数author_can() (https://www.wp-admin.cn/) WordPress开发教程 第1张
用户贡献的笔记

(由Codex – 5年前贡献)

例子

if ( author_can( $post->ID, ‘publish_posts’ ) ) {

echo __( ‘Yes they can publish posts!’, ‘textdomain’ );

}

相关推荐

发表回复

房先生
我们将24小时内回复。
2024-07-15 10:45:51
您好,有任何疑问请与我们联系!
您的工单我们已经收到,我们将会尽快跟您联系!
[QQ客服]
2781198
加我微信
[电话联系]
13585372227
[电子邮件]
chaoneo@163.com
取消

选择聊天工具: