WordPress 登录才能查看评论模块

首页 » 实用教程 » WordPress 登录才能查看评论模块
所属分类:实用教程
发布时间:2020-02-17
浏览次数:3,716 views

WordPress正常可以设置登录发表评论,但不登录也可以正常看到留言评论内容,最近有用户说接到通知个人备案的网站不允许有评论互动功能,虽然我没接到过通知,但可以简单修改一下模板,让主题评论模块只有在登录的状态下可见。

这里我们要用到WordPress判断是否登录的函数:is_user_logged_in()

用判断函数把评论模块包裹起来就行了。

以WordPress默认主题Twenty Seventeen为例,打开主题正文模板文件single.php,找到类似的:

if ( comments_open() || get_comments_number() ) :
	comments_template();
endif;

修改为:

if ( is_user_logged_in()){
	if ( comments_open() || get_comments_number() ) :
		comments_template();
	endif;
}

之后,只有登录的状态下才能看见评论模块及评论内容。

其它主题方法类似,比如:

<?php if ( is_user_logged_in()){ ?>
<?php if ( comments_open() || get_comments_number() ) : ?>
	<?php comments_template( '', true ); ?>
<?php endif; ?>
<?php } ?>
关键字:

相关阅读

WP建站统计用户搜索关键词数据的插件 – Search Keyword Statistics

站长朋友们在使用WordPress做网站的过程中,经常会在网站中加入搜索的功能,希望能够统计出访客在自己的网站 […]

WP新版本如何禁用古腾堡(Gutenberg)使用经典的编辑器

很多人在使用 WordPress 最新的 Gutenberg 编辑器时都觉得并不适应,比较难上手(PS:其实还 […]

WordPress网站插件和functions.php哪个更好?

我们经常被用户询问他们是否应该安装一个WordPress插件或者将代码添加到他们主题的functions.ph […]