比较职位级别和用户级别意愿列表成员

时间:2013-02-27 作者:idontknowhow

我正在使用Wishlist成员插件,我正在构建一个比较user\\u级别和post\\u级别的函数,但我无法让我的函数正常工作:

check_user_access($userid, $postid){

     // get user_levels
     $user_levels = WLMAPI::GetUserLevels($userid);

     // get the post levels
     $post_levels = WLMAPI::GetPostLevels($postid);

     // Compare user_levels with post_level
     $result = array_search($user_levels, $post_levels);

     if ($result === false) {
         return false;
     }
     else {
         return true;
     }

 }

3 个回复
SO网友:jfacemyer

可能是因为$user\\u levels是一个数组?

尝试以下操作:

// Compare user_levels with post_level
$result = \'\';
foreach($user_levels as $user_level) {
    $result .= array_search($user_level, $post_levels);
}
if ($result) {
    return true;
}

SO网友:John Morris

我想这就是你要找的。这将让您找到用户是否可以访问帖子、页面或类别。这将使用最新版本的WishList成员中的新API函数,并接受“post”、“page”或“category”作为$object\\u类型参数。

function member_can_access($user_id, $object_type, $object_id) {
    $levels = wlmapi_get_member_levels($user_id);

    $map = array(
        \'post\' => \'wlmapi_get_level_posts\',
        \'page\' => \'wlmapi_get_level_pages\',
        \'category\' => \'wlmapi_get_level_categories\'
    );

    $plurals = array(
        \'post\' => \'posts\',
        \'page\' => \'pages\',
        \'category\' => \'categories\'
    );

    foreach ( $levels as $level ) {
        $objects[] = call_user_func($map[$object_type], $level->Level_ID);
    }

    foreach ( $objects as $object ) {
        foreach ( $object[$plurals[$object_type]][$object_type] as $item ) {
            $items[] = $item[\'ID\'];
        }
    }

    $items = array_unique($items);

    if ( !in_array( $object_id, $items ) ) {
        return false;
    }

    return true;
}

?><pre><?php print_r(member_can_access(1, \'post\', 1)); ?></pre>
您可以在以下位置查看所有已使用函数的文档:http://codex.wishlistproducts.com

SO网友:Bob Tolbert

首先,您使用的是旧的Wishlist成员API,尝试使用新的API,它更全面。您可以在以下位置找到新API:http://wishlistproducts.com/api/api2/

返回的变量是对象,用于查找其内容用途var_dump 对于调试,只需获取所需信息即可。

结束

相关推荐

Functions.php:从博客中排除类别

所以很明显,如何从模板中排除某些类别,但我不想修改4个模板,使它们忽略某个类别。有没有一种方法可以将某个类别从阅读设置的“博客”集中排除?我正在将博客分配到名为“博客”的页面。。。但显然,档案和搜索也需要对这一超出类别的内容视而不见。我宁愿在里面做functions.php