为具有特定角色的用户显示文本
Display text for users with certain roles
我在自定义页面中有一段文字--front.tpl.php 文件。我想将其包装在 php if 语句中,以便它仅显示给我的两个站点角色。我有以下内容,但它只显示 "client" 角色,而我也想为 "consultants" 角色显示它。
<?php if (in_array('client', $GLOBALS['user']->roles)):?>
客户资料
in_array 函数应该接受多个搜索参数(如果它们作为数组传递)。所以它应该是这样的:
<?php if (in_array(array('client','consultants'), $GLOBALS['user']->roles)):?>
但如果这不起作用(应该),您可以随时使用 or 语句:
<?php if (in_array('client', $GLOBALS['user']->roles) || in_array('consultants', $GLOBALS['user']->roles)):?>
更新: 似乎 in_array()
不能接受第一个(针)参数的数组。在堆栈溢出上查看此线程:
Checking to see if one array's elements are in another array in PHP
所以 array_intersect()
函数应该是您正在寻找的。
我在自定义页面中有一段文字--front.tpl.php 文件。我想将其包装在 php if 语句中,以便它仅显示给我的两个站点角色。我有以下内容,但它只显示 "client" 角色,而我也想为 "consultants" 角色显示它。
<?php if (in_array('client', $GLOBALS['user']->roles)):?>
客户资料
in_array 函数应该接受多个搜索参数(如果它们作为数组传递)。所以它应该是这样的:
<?php if (in_array(array('client','consultants'), $GLOBALS['user']->roles)):?>
但如果这不起作用(应该),您可以随时使用 or 语句:
<?php if (in_array('client', $GLOBALS['user']->roles) || in_array('consultants', $GLOBALS['user']->roles)):?>
更新: 似乎 in_array()
不能接受第一个(针)参数的数组。在堆栈溢出上查看此线程:
Checking to see if one array's elements are in another array in PHP
所以 array_intersect()
函数应该是您正在寻找的。