WordPress 正常可以设置登录发表评论,但不登录也可以正常看到留言评论内容,我们可以简单修改一下模板,让主题评论模块只有在登录的状态下可见。
这里我们要用到 WordPress 判断是否登录的函数:is_user_logged_in(),用判断函数把评论模块包裹起来就行了。
以 WordPress 默认主题 Twenty Seventeen 为例,打开主题正文模板文件 single.php,找到类似的:
if ( comments_open() || get_comments_number() ) :
comments_template();
endif;
comments_template();
endif;
修改为:
if ( is_user_logged_in()){
if ( comments_open() || get_comments_number() ) :
comments_template();
endif;
}
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 } ?>
<?php if ( comments_open() || get_comments_number() ) : ?>
<?php comments_template( ”, true ); ?>
<?php endif; ?>
<?php } ?>
ripro主题的 single.php 文件在 parts/template-parts 目录下 content-single.php,最后的位置。
请先
!