![](https://i-blog.csdnimg.cn/blog_migrate/7f18726a5c42a53735f950988f40edb5.png)
隐藏wordpress后台
WordPress allows you to create password protected posts. Recently one of our readers asked if it was possible to hide password protected posts from the site. By default, WordPress hides the content of a password protected post, but it still shows the post title with ‘Protected’ prefix. In this article, we will show you how to hide password protected posts from the WordPress loop.
WordPress允许您创建受密码保护的帖子 。 最近,我们的一位读者询问是否可以从该网站隐藏受密码保护的帖子。 默认情况下,WordPress隐藏密码保护帖子的内容,但仍显示带有“ Protected”前缀的帖子标题。 在本文中,我们将向您展示如何从WordPress循环中隐藏受密码保护的帖子。
为什么要在WordPress中隐藏受密码保护的帖子? (Why Hide Password Protected Posts in WordPress?)
By default, WordPress displays the password protected post with its title and a ‘protected’ prefix. Users will need to enter the password to view the content of the post.
默认情况下,WordPress显示带有标题和“保护”前缀的受密码保护的帖子 。 用户将需要输入密码才能查看帖子的内容。
![Password protected posts displayed on homepage and in widgets Password protected posts displayed on homepage and in widgets](https://i-blog.csdnimg.cn/blog_migrate/75c5c2b3b59d362f4d04bc1c425beeba.png)
This post title is visible on the homepage, archives, recent posts widget, etc. If you want to keep some content completely private, then this is not a ideal.
该帖子标题可在主页,存档,最近发布的窗口小部件等上看到。如果您希望将某些内容完全保密,那么这是不理想的。
Not only users who don’t have password can see the post title, they can also try to enter passwords. As we all know, passwords can be cracked.
没有密码的用户不仅可以看到帖子的标题,还可以尝试输入密码。 众所周知,密码可能会被破解。
Having said that, let’s take a look at how to hide your password protected posts from WordPress loop so that other users cannot see them.
话虽如此,让我们看一下如何在WordPress循环中隐藏受密码保护的帖子,以便其他用户看不到它们。
在WordPress中隐藏受密码保护的帖子 (Hiding Password Protected Posts in WordPress)
Simply add this code to your theme’s functions.php file or a site-specific plugin.
只需将此代码添加到主题的functions.php文件或特定于站点的插件中即可 。
function wpb_password_post_filter( $where = '' ) {
if (!is_single() && !is_admin()) {
$where .= " AND post_password = ''";
}
return $where;
}
add_filter( 'posts_where', 'wpb_password_post_filter' );
This code simply modifies the query sent to the WordPress by using the posts_where
filter. It asks WordPress to fetch all posts that do not have a password.
此代码仅使用posts_where
过滤器修改发送到WordPress的查询。 它要求WordPress提取所有没有密码的帖子。
Visit your website and you will see that password protected posts are no longer visible on homepage, archives, or in widgets like recent posts.
访问您的网站,您将看到受密码保护的帖子不再显示在主页,存档或诸如最新帖子之类的小部件中。
![Before and after hiding protected posts in WordPress Before and after hiding protected posts in WordPress](https://i-blog.csdnimg.cn/blog_migrate/69546d288408042a215717b2856284a5.png)
You can still visit the post by accessing it through a direct URL to the post itself.
您仍然可以通过直接访问帖子本身的URL来访问该帖子。
The example above, hides password protected posts from all users. What if you ran a multi-author WordPress site and wanted protected-posts to be viewable by users with the capability to edit private posts?
上面的示例对所有用户隐藏受密码保护的帖子。 如果您运行了一个多作者 WordPress网站并希望受保护的帖子可供具有编辑私人帖子功能的用户查看,该怎么办?
Simply modify the above code with another conditional tag, like this:
只需使用另一个条件标签修改以上代码,如下所示:
function wpb_password_post_filter( $where = '' ) {
if (!is_single() && !current_user_can('edit_private_posts') && !is_admin()) {
$where .= " AND post_password = ''";
}
return $where;
}
add_filter( 'posts_where', 'wpb_password_post_filter' );
In this example, we check if a user cannot edit the password protected posts, then only show the posts that don’t have password. Doing so all users with user roles of administrator and editor will see the password protected posts on the front end of your site.
在此示例中,我们检查用户是否无法编辑受密码保护的帖子,然后仅显示没有密码的帖子。 这样做,所有具有管理员和编辑者角色的用户都将在网站的前端看到受密码保护的帖子。
We hope this article helped you hide password protected posts from WordPress loop on your site. You may also want to see our tutorial on how to change private and protected posts prefix in WordPress.
我们希望本文能帮助您从站点上的WordPress循环中隐藏受密码保护的帖子。 您可能还想看一下我们的教程,以了解如何在WordPress中更改私人和受保护帖子前缀 。
If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Google+.
如果您喜欢这篇文章,请订阅我们的YouTube频道 WordPress视频教程。 您也可以在Twitter和Google+上找到我们。
翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-hide-password-protected-posts-from-wordpress-loop/
隐藏wordpress后台