Page 1 of 1

Listing Categories

PostPosted: April 8th, 2011, 2:48 pm
by vitorclaw
Hey guys, first I would like to say that s2member is an amazing plugin really easy and simple to customize. You guys must be proud of it.

Now my question:

I have a blog where I display the 10 latest posts, and on the sidebar I have a list with all my categories generated by wp_list_categories();

The thing is, I would like to exclude categories that are blocked for a certain type of user. (I'm currently using the Alternative view protection, but it doesn't affect this function)

Is there any way to do that?

What i could do if there isn`t any avaiable option to do that is to get all Categories that are blocked ID and insert it into the function so it would be something like:
$blocked_cats;
wp_list_categories("exclude=$blocked_cats");

But I don`t really know how to get those categories.

So I hope you guys can help me. Thanks

Re: Listing Categories

PostPosted: April 8th, 2011, 2:58 pm
by vitorclaw
Nevermind, just found the solution, for future reference:

Example #4: Checking Category Restrictions.

Code: Select all
<?php
if 
($categories = get_categories ())
    {
        foreach ($categories as $category)
            {
                if (!is_permitted_by_s2member ($category->cat_ID, "category"))
                    continue;
                /* Skip it. The current User/Member CANNOT access this Category,
                or any Posts inside this Category, or any of its sub-Categories. */
            }
    }
?>


EDIT:

By the way, the only way to display categories that i found is doing something like this:

Code: Select all
if ($categories = get_categories ()) {
    $i = 0;
    foreach ($categories as $category) {
        if (!is_permitted_by_s2member ($category->cat_ID, "category"))
            continue;
        $variables = get_object_vars($category);
        echo $variables['name'];
        $i++;
    }
}
 

Re: Listing Categories

PostPosted: April 8th, 2011, 8:11 pm
by Cristián Lávaque
I'm glad you found the solution. :)

I'm just curious, what do you need the $i there for?

For the benefit of others: that example can be found under WP Admin -> s2Member -> API / Scripting -> Advanced Query Conditionals.

Re: Listing Categories

PostPosted: April 8th, 2011, 8:26 pm
by vitorclaw
Hi Cristián, theres no need for it. I was trying another solution before and just forgot to remove it hehe.

Re: Listing Categories

PostPosted: April 8th, 2011, 8:56 pm
by Cristián Lávaque
LOL I know what you mean. :)