将类别限制为仅登录用户

时间:2017-05-16 作者:heroj

我只有前端块,我需要将类别限制为仅登录用户,因此作者创建类别和此类别仅查看当前作者,而不是其他用户。wordpress中有什么方法/插件吗?

edit1:我的问题是simmilar,比如:Restricting Users to view only Custom Taxonomies they have entered?但我不知道它是如何实现的。

edit2:我使用此插件将meta terms字段添加到taxonomie:https://fuc.wordpress.org/plugins/wp-custom-taxonomy-meta/因此,我将用户元与分类法(category)相比较。如何实现get\\u terms和wp\\u dropdown\\u cats,并仅为当前登录的作者(前端用户)显示分类?

1 个回复
SO网友:Rishabh

单圈内部while循环。php您可以这样放置条件

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
}}
这可以为其他主题文件模板完成,就像我们为single所做的一样。php。

UPDATE

如果你想让一个用户看不到另一个用户的帖子,那么你可以使用这两个函数来实现这一点

  • get_the_author(): 获取帖子作者的显示名称wp_get_current_user(): 要获取登录用户的信息,请使用以下while循环

    //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
    
    }
    
    您还可以将if条件放在ID上,而不是显示名称上。为此,您需要对上述代码进行2次更改

    更换get_the_author() 具有的函数get_the_author_meta()

  • 并替换以下代码行

    $current\\u user\\u info=$current\\u user->display\\u name;

    使用此

    $current_user_info = $current_user->ID; 
    
    要了解更多信息,请在我的链接中详细阅读此函数。

结束

相关推荐

Categories' hierarchy in URL

我目前正在处理的网站中的帖子都有多个层次分类。例如:Source - Books -- Moby Dick -- Sherlock Holmes 永久链接设置为/%category%/%postname%/. 然而,一篇文章的URL并不包括所有的子类别——我得到的只是site.com/source/books/*postname*, 尽管这篇文章在来源上没有分类,但只在书籍+白鲸上。有人能帮我找出如何调整这种行为吗?非常感谢。