根据用户登录显示内容

时间:2014-08-29 作者:user3756781

我想知道这是否可能。我创建了一个客户端门户,在那里他们可以看到分配给他们的所有工作(这些都显示在前端)。一旦他们登录,他们就会被重定向到他们的页面。我正在使用下面的代码显示与当前用户相关的项目

 <?php if ( is_user_logged_in() ) : function filter_posts_by_author( $query ) { 
        global $current_user; get_currentuserinfo();
        $query->set( \'author\', $current_user->ID );
      }
        add_action( \'pre_get_posts\', \'filter_posts_by_author\' );?> 
 <h2>Post Goes here</h2>
 <?php else: wp_die(\'Sorry, you do not have access to this page. Please <a href="/#/">sign in</a> to view this page.\');endif; ?>
这很好用。我试图解决的问题是,当您以管理员身份登录时,如何显示所有用户的所有帖子(在前端)

我试过了,但它会向所有客户端和管理员显示所有帖子

 <?php if ( is_user_logged_in() ) : function filter_posts_by_author( $query ) { 
        global $current_user; get_currentuserinfo();
        $query->set( \'author, administrator\', $current_user->ID );
      }
        add_action( \'pre_get_posts\', \'filter_posts_by_author\' );?> 
当管理员登录时,如何显示所有帖子,而当用户登录时,如何仅显示所选帖子?

1 个回复
最合适的回答,由SO网友:Howdy_McGee 整理而成

您可以尝试这样做,如果当前用户已登录而不是管理员,它将从当前用户中提取所有帖子,否则它将默认显示并显示所有用户的所有帖子。

<?php 
    function filter_posts_by_author( $query ) {
        if( is_user_logged_in() ) {
            if( !current_user_can( \'administrator\' ) ) {
                global $current_user; 
                get_currentuserinfo();
                $query->set( \'author\', $current_user->ID );
            }
        }
        else {
            wp_die(\'Sorry, you do not have access to this page. Please <a href="/#/">sign in</a> to view this page.\');
        }
    }
    add_action( \'pre_get_posts\', \'filter_posts_by_author\' );
?>

结束

相关推荐

3rd Party Login in wordpress

我有两个网站。一个在wordpress上,一个在非wordpress网站上。现在,我已经使用了非wordpress站点的页眉和页脚。我想要的是,如果用户登录到我的非wordpress站点,那么他可以使用相同的电子邮件和名称在我的wordpress站点上发表评论。我通过CURL请求从用户的非wordpress站点获取用户信息。我面临的主要问题是如何使用用户的电子邮件和姓名发表评论。我试着用这个:global $current_user; $current_user->user_email =