Home Experience & Tips Sharing Standard Post

Standard Post

Method to Hide WordPress Comment Module, Only Visible to Logged-in Users

Due to domestic policy, personal filing does not allow websites to have comment functionality, but a personal blog without comment functionality seems soulless, so we can set it so that only logged-in users see the comment area content. The specific method is as follows: Find the single.php file of your theme, for example, the Twenty Seventeen theme, find code similar to the following...

Posted on January 10, 2020 About 2 minutes read
隐藏WordPress评论模块,只有登录用户才可查看的方法

Due to domestic policy, personal filing does not allow websites to have comment functionality, but a personal blog without comment functionality seems soulless, so we can set it so that only logged-in users see the comment area content. The specific method is as follows:

Find the single.php file of your theme, for example, the Twenty Seventeen theme, find code similar to the following.

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

Modify it as shown below:

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

The method for other themes is similar; just find the corresponding code and add a conditional check.

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

if is_user_logged_in determines whether the user is logged in. If logged in, the comment module is displayed; if not logged in, it will not load.

Of course, after making this modification, you need to enable user registration on your website. You might also be interested in the following articles:

The code in this article was shared by Zhigengniao.

Rate this article post
Previous Shopify domain setup tutorial: Resolving your own domain to Shopify Continue reading content around the same timeline. Next How to Write Webpage Titles and Descriptions to Improve Google SEO Ranking View the next related tutorial or experience.

AI Website Building Assistant

🤖
Hello! I am the Naibabiji AI Assistant. How can I help you?
Quick Consultation: