categories - restrict category to only logged in User

admin2025-01-07  6

I have only frontend block, and I need restrict category to only logged in User, so The Author create category and this category view only this current author, not another user. Its here any way/plugin in wordpress?

edit1: My question is simmilar, like here: Restricting Users to view only Custom Taxonomies they have entered? but I don´t know how it implement for my.

edit2: I add meta terms field to taxonomie with this plugin: / So I parrent user meta with taxonomie (category). How can I implement get_terms and wp_dropdown_cats and show taxonomie for only current logged in Author (Frontend User)?

I have only frontend block, and I need restrict category to only logged in User, so The Author create category and this category view only this current author, not another user. Its here any way/plugin in wordpress?

edit1: My question is simmilar, like here: Restricting Users to view only Custom Taxonomies they have entered? but I don´t know how it implement for my.

edit2: I add meta terms field to taxonomie with this plugin: https://fuc.wordpress.org/plugins/wp-custom-taxonomy-meta/ So I parrent user meta with taxonomie (category). How can I implement get_terms and wp_dropdown_cats and show taxonomie for only current logged in Author (Frontend User)?

Share Improve this question edited May 20, 2017 at 22:55 heroj asked May 16, 2017 at 23:30 herojheroj 238 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 0

Inside while loop of single.php you can put condition like this

if(in_category('Catetory_slug')){ //Specifying category whom we don't want to see by no logged in users
    if(is_user_logged_in()){      //Checking if user logged in or not
        //code inside single.php
    }else{
        Echo "You need to login to view this post."; //message for no logged in users
}}

This can be done for other template of theme files like we did for single.php.

UPDATE

If you want one user couldn't see posts of another user then you can use these two functions to achieve this

  • get_the_author(): To get display name of author of post
  • wp_get_current_user(): To get info of logged in user

Use following inside while loop

//storing autor's display name in variable
$post_author = get_the_author();    
$current_user = wp_get_current_user();
//storing autor's display name in variable
$current_user_info = $current_user->display_name;
//Chacking if bot strings (display names) are equal or not
if (strcmp($post_author, $current_user_info) == 0) {  

     //Your Code to display post's content

}

You can also put if condition on ID instead of display name. For that you need to make 2 changes in above code

  1. replace get_the_author() function with get_the_author_meta()
  2. and replace following line of code

    $current_user_info = $current_user->display_name;

with this

$current_user_info = $current_user->ID; 

To know more read about this functions in detail in my given link.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736264579a979.html

最新回复(0)