php - 如何仅向具有特定角色用户的登录用户提供内容?
问题描述
直到现在我找到了这个代码,但这仅适用于登录或未登录。我也想添加一个角色过滤器。
任何人都可以帮我做吗?
<?php if ( is_user_logged_in() ) : ?>
<My Content>
<?php else : ?>
<Log in to Watch Content>
解决方案
这是你的答案:
<?php $user = wp_get_current_user();
if ( in_array( 'admin', (array) $user->roles ) ) : ?>
<Your Content>
<?php else : ?>
<Log in to Watch Content>
<?php endif; ?>
推荐阅读
- javascript - React Native:分别证明弹性项目(子项)的合理性?
- javascript - 从使用 combineReducers 的 Redux 主状态中删除特定属性的正确方法 - React Redux
- ios - 在 podfile 中配置 ML Kit 库模型的步骤 - Flutter Apps
- c# - 是否可以在 DB ASP.NET 的会话存储中存储额外的数据?
- reactjs - 如果我运行多重测试,getByText 会失败,但如果我单独运行,它会起作用
- python - 将 DatasetV1Adapter 形状灰度图像形状扩展到 3 个通道,以利用预训练模型
- javascript - 如何将用户从一个 URL 重定向到另一个具有 JavaScript 操作(如添加到购物车)的网站
- php - Wordpress 在其他自定义帖子永久链接中的一个自定义帖子永久链接结构
- c# - 查找复杂导航网格的最近点
- php - require_once '/AmazonPay/Client.php' 的完整路径是什么?