纯代码实现 WordPress 评论模块登录可见

来自:互联网
时间:2020-02-21
阅读:

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;
}

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

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

<?php if ( is_user_logged_in()){ ?>
<?php if ( comments_open() || get_comments_number() ) : ?>
<?php comments_template( '', true ); ?>
<?php endif; ?>
<?php } ?>
返回顶部
顶部