admin管理员组文章数量:1336590
I'm working on a site for a school and the teachers all want to post the homework for their students online, but only so each student can see his or her own class's homework posted by their teacher.
What i want to do is basically this:
- get user role
- if user role = rolename1, display posts by category 1, hide all other posts
- if user role = rolename2, display posts by category 2, hide all other posts
- and so on for the 22 different user roles
All of these user roles are subscriber levels, I just have them broken down this way as it was the easiest way to separate them into categories.
Ideally I want to have one blog page where the feed for all the posts for the site are displayed and you only see the ones that are appropriate for you within this feed. Everything else is hidden.
I also want to restrict each author to using only the categories I assign them rather than choosing from all the categories available.
Any ideas? Snippets or plugin recommendations would be highly appreciated.
I'm working on a site for a school and the teachers all want to post the homework for their students online, but only so each student can see his or her own class's homework posted by their teacher.
What i want to do is basically this:
- get user role
- if user role = rolename1, display posts by category 1, hide all other posts
- if user role = rolename2, display posts by category 2, hide all other posts
- and so on for the 22 different user roles
All of these user roles are subscriber levels, I just have them broken down this way as it was the easiest way to separate them into categories.
Ideally I want to have one blog page where the feed for all the posts for the site are displayed and you only see the ones that are appropriate for you within this feed. Everything else is hidden.
I also want to restrict each author to using only the categories I assign them rather than choosing from all the categories available.
Any ideas? Snippets or plugin recommendations would be highly appreciated.
Share Improve this question asked Feb 2, 2019 at 7:28 sahiba25sahiba25 212 silver badges3 bronze badges 2- Wouldn't it be easier to skip the category altogether, and display posts of a particular author based on a role? – Bram Commented Feb 2, 2019 at 10:11
- 1 Well, it would be, but some of the authors have more than one class so then their pages would show information for all of their classes on one page and we don't want anyone from one class to see info from any other classes. – sahiba25 Commented Feb 4, 2019 at 19:45
2 Answers
Reset to default 1The problem consists of three parts: getting the current user data, linking that to a category and retrieving and displaying the posts. You can include these snippets on a custom page template, which you assign to the 'My homework'-page.
Below these three sub-answers, I share some additional suggestions on ways of making this more user-friendly / user-manageable.
Getting the user data
This is done very easily using wp_get_current_user
. This yields a WordPress User object, from which you can retrieve the roles using $current_user->roles
(assuming you have set $current_user = wp_get_current_user();
). This yields an array. I'm assuming the users only have one role, so that the first element in that array is the element we're checking against.
Linking users to categories
You can do this using an if-elseif
-statement, as follows.
$roles = $current_user->roles;
if ($roles[0] == 'role'){
$category_name = 'category-name';
}elseif ($roles[0] == 'other-role'){
$category_name = 'other-category-name';
}
You can extend this as you like, by adding additional elseif
-clauses. Replace role
(and other-role
) by the slugs of the roles, and category-name
and other-category-name
by the slugs of the categories.
Retrieving and displaying the posts
Using $category_name
in the arguments of a WP_Query
limits the result that query retrieves to the categories.
$qrt_args = array(
'category_name' => $category_name
);
$posts = new WP_Query( $qry_args );
You can now use $posts
in a regular loop and display the results.
Other ideas
Although the code above should work (haven't tested it), it requires you to edit code in order to make adjustments. That might be less convenient, for example if you at some point in time, are unavailable to make these changes.
Another option is to create a post for each student, subsequently assign the relevant category (or categories) to that post and set the author as the student. This way, you can match students and categories in the Wordpress Dashboard. Also, this provides a little more flexibility and robustness, for example to assign multiple categories to the same student. (The code posted above assumes only one category.) Of course, this will also display the post you've used to set up the match on the page with homework problems, but you can use that to your benefit ("Hey John, please find your homework problems below"). It is, however, also possible to prevent that from happening using an additional category or tag.
Another option, which is even nicer but also a bit more difficult to implement, is to create a new usermeta-field in which you save which categories of posts that user should be able to see.
Please note this does not (yet) provide a solution to this part of your problem:
I also want to restrict each author to using only the categories I assign them rather than choosing from all the categories available.
You can try using WP Private Content Plus plugin. It allows you to restrict your content to specific user roles or or group of selected users. You just need to use the shortcode.
For multiple user roles, you have to use this shortcode:
[wppcp_private_content allowed_roles="subscriber,editor" ]
Private Content for Users with
Subscriber or Editor User Role
[/wppcp_private_content]
本文标签: loopDisplay posts of certain categories to specific user roles
版权声明:本文标题:loop - Display posts of certain categories to specific user roles 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742410460a2469680.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论