仅当作者登录时才在单个页面上显示他们的帖子

时间:2014-06-11 作者:bigant841

我很难找到一个解决方案或正确的教程如何做到这一点,我已经寻找了几个星期,但仍然没有结果。我试图完成的是在登录的作者(作者A)的前端(single.php)显示所有帖子。当作者A注销single上的所有帖子时。php将被删除,并留下一条消息(您必须登录才能查看此页面)。然后,当Author B登录到所有Author B post时,将在single上显示。php。每个帖子都是由一个名为projects的自定义帖子类型生成的,该类型可以指定作者。我正在尝试在没有插件的情况下实现这一点。

任何帮助都将不胜感激。

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

检查用户是否使用登录is_logged_in. 如果用户已登录,请使用修改查询pre_get_posts 按当前作者ID筛选帖子。

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\' );
  // ... loop
else : 
  echo \'you must be logged in to view this page\';
endif;

结束

相关推荐

Featured posts and the loop

我正在尝试在索引中添加一个框。php在何处显示最新的3篇特色帖子。根据this tutorial, 我已将以下内容添加到functions.php:add_theme_support( \'featured-content\', array( \'filter\' => \'magdeleine_get_featured_posts\', \'max_posts\' => 3, ) ); function magdeleine