Before this, I’ve shown how to disable comments section on post or page. Please note that removing and disabling the comments section are two different things. For example, when you decide not to have a comments section for posts from certain categories or post formats, completely remove the comments section is the best choice so every time you want to write a post for those categories or post formats, you do not have to untick the box from the discussions setting to disable comments.
In this simple tutorial, I’ll show you how to conditionally remove comments section. In one of the projects I did for a client, I need to remove the comments section for posts in link and video post format. Instead of having to disable the comments section every time when the client wants to write a post in the link or video post format, I want to make it easier for him. When the client choose the link or video post format, the comments section will automatically disabled. In other words, the comments section isn’t there since I’ve completely removed it.
Please note that this code is only can be used if you’re using Genesis Framework.
Here is the code to remove the comments sections site-wide:
// Remove post comments add_action( 'wp_enqueue_scripts', 'custom_remove_comments' ); function custom_remove_comments() { remove_action( 'genesis_after_post', 'genesis_get_comments_template' ); }
Thanks to Brian, the founder of StudioPress for the snippet.
Remove Comments Section on Post Format
And here’s the code I used for the project. What the code does is remove the comments section when the post is link or video post format.
// Remove post comments post format add_action( 'wp_enqueue_scripts', 'custom_remove_comments' ); function custom_remove_comments() { if ( 'link' == get_post_format() || 'video' == get_post_format() ) { remove_action( 'genesis_after_post', 'genesis_get_comments_template' ); } }
Sky is the limit. Remember, WordPress has tons of useful conditional tags. Use any of them according to your needs.
Leave a Reply