To enable comments on Divi blog post pages, you can use the comments_template()
function provided by WordPress. This function loads the comments template for the current post.
Here’s an example code snippet that you can add to your Divi child theme’s single.php
file to display comments on blog post pages:
<?php
// Check if comments are open or if there are comments already
if (comments_open() || get_comments_number()) {
comments_template();
}
?>
This code snippet checks if comments are open for the current post or if there are already comments. If either condition is true, it loads the comments template using the comments_template()
function.
By adding this code to your Divi child theme’s single.php
file, you ensure that comments are displayed on your Divi blog post pages.
Note: It’s important to have the comments_template()
function wrapped within the conditional statement to prevent the comments template from being loaded unnecessarily.
This code snippet is useful for any Divi website that wants to enable comments on their blog post pages. It follows the WordPress coding standards and ensures that the comments template is only loaded when necessary.
Examples
Example 1: Disable comments on Divi blog posts
This example demonstrates how to disable comments on Divi blog posts by removing the comment template from the single post template file.
function wpsnippets_disable_comments_on_divi_blog_posts() {
if (is_singular('post') && comments_open()) {
comments_template('', false);
}
}
add_action('template_redirect', 'wpsnippets_disable_comments_on_divi_blog_posts');
This code snippet checks if the current page is a single blog post and if comments are open. If both conditions are met, it removes the comment template from the page, effectively disabling comments on Divi blog posts.
Example 2: Customize comment form on Divi blog posts
This example demonstrates how to customize the comment form on Divi blog posts by modifying the comment_form() function.
function wpsnippets_customize_comment_form($fields) {
// Modify the comment form fields here
return $fields;
}
add_filter('comment_form_default_fields', 'wpsnippets_customize_comment_form');
function wpsnippets_customize_comment_form_submit_button($submit_button) {
// Modify the comment form submit button here
return $submit_button;
}
add_filter('comment_form_submit_button', 'wpsnippets_customize_comment_form_submit_button');
This code snippet uses two separate filter hooks to customize the comment form on Divi blog posts. The comment_form_default_fields
filter allows you to modify the comment form fields, while the comment_form_submit_button
filter allows you to modify the submit button.
Example 3: Display comments count on Divi blog posts
This example demonstrates how to display the comments count on Divi blog posts by adding a custom function to the theme’s functions.php file.
function wpsnippets_display_comments_count() {
if (comments_open()) {
$comments_count = get_comments_number();
if ($comments_count == 0) {
echo 'No comments';
} elseif ($comments_count == 1) {
echo '1 comment';
} else {
echo $comments_count . ' comments';
}
}
}
This code snippet checks if comments are open on the current page and retrieves the number of comments using the get_comments_number()
function. It then displays the appropriate message based on the number of comments.