Information


Customizing Comments in Wordpress a Template

In Wordpress, Pages are designed to be static, with their own menu system that is quite separate from posts. When editing the page, you can allow comments in the Discussion options, however, many templates will not display comments for a page.

This article explains how to add comments to a page, and how to modify how comments are handled when the allow comments option for a page has been disabled.

Part of any standard Wordpress template package is the comments.php file, which controls how comments are displayed on your site.

I just launched a new template for my site Dougs-Travels.com.  It looks a lot better, but it isn't perfect.

I got a comment from Justin left a comment saying that the method for leaving comments was not so intuitive.

This lead me to two realizations:

  1. I need to make it easier for people to leave comments from the main page
  2. I need a place for people to leave comments that are not directly related to a post about the travelling experiences I write about.

So, I just add a page called feedback, and enable comments, right?

Actually, no.  The template I started out with did not allow for comments on a page.

Adding comments was easy though.  I just added the following code at the point at which I wanted comments and the form that adds them to appear:

    <?php comments_template(); ?>

That worked, but it caused another problem: on pages where I have comments disabled, a message appeared informing the reader that comments are closed.  Actually, this is not what I want!  I either want comments, or nothing.

To fix it, I needed to fix the comments.php file for the template system.  This file is responsible for displaying comments and accepting new comments.  I added the following code around anything to do with comments that should disappear if comments are disabled for a particular page:

 <?php if ('open' == $post->comment_status) : ?>
    <!– do something if comments are enabled –>
<?php else: endif; ?>

The above code produces no output if comments are disabled, so you can use the following code to display a heading for comments, if and only if comments are enabled by the editor of a page:

 <?php if ('open' == $post->comment_status) : ?>
    <h3>Comments:</h3>
<?php else: endif; ?>

 


Comments

3 comments:

Thanks for this - solved a problem for me :-)

Will James / November 22nd, 2006, 2:33 pm / #

[…] 1. You can now discuss/comment directly on all articles. I’ve been trying to do this for ages, but finally found out how with help from here. […]

Carpcast - Carp Fishing Blog :: A small update / November 22nd, 2006, 2:36 pm / #

Thanks, this sorted out the problem for me. I was having an “Aaaah..!” moment when I realized that I had posted some stuff and that comments weren’t featured on the pages. Thanks again.

michael reed / February 21st, 2007, 6:24 pm / #

Post a comment