How to Add a JomComment Comment Counter to Your Joomla-Powered Blog
This article is about adding a comment counter to a Joomla-powered blog with JomComment. Besides having the usual "Add Comment (2)" link at the end of each article introduction, it allows to add a comment counter and link along with the "Written by…," date, section, and category.
With the ability to override Joomla’s core content output in a Joomla 1.5 template, you can do virtually anything within your template. This tutorial demonstrates how to use template overrides to insert a JomComment comment counter anywhere you want to add it in your articles.
1. Override the content component output in your template
This section will give you a brief overview of the process of how to override the content component’s output in your template. The Joomla 1.5 framework gives you the ability to override the HTML output of any core component and third-party components that are built to support this feature. The Beez template that comes pre-installed with Joomla 1.5 is a great example of what can be done with template overrides, and the best place to start if you are building a template from scratch is to copy the "html" folder from the beez template to your template. (Warning: doing this could dramatically change the appearance of your site, so please try this out on a test server before attempting it on a live site!)
Once you have copied the html folder from the Beez template folder to your template folder, the template overrides for the content component can be found at "yoursite.com/templates/yourtemplate/html/com_content". There are four files that you need to work with in order to add the JomComment comment: counter:
2. Get the number of comments for the article
The following counter code comes from the JomComment component. Add this code anywhere in each of the four files listed above.
require_once ( JPATH_BASE .DS.’components’.DS.’com_jomcomment’.DS.’helper’.DS.’minimal.helper.php’ );
$jcCount = jcCountComment($this->item->id);
This little bit of code does two things. First, it references one of JomComment’s helper php files that contains a function, jcCountComment, that counts the number of comments for the article whose ID is passed as a parameter to the jcCountComment function. The second line gets the number of comments for the article by passing the article’s ID as $this->item->id, and it stores that number in the variable $jcCount.
3. Display the number of comments and link the number to the comment form
Now that you have the number of comments for the article, you’re halfway there. You still need to link the number to the comment form for the article. Here is the example of the code used to link to the comment form:
<img src="templates/htj_960/images/icon-addcomment.gif" width="15" height="14" alt="Add Comment" title="Add Comment" style="vertical-align: middle;" />
<a href="<?php echo $this->item->readmore_link; ?>#comments" class="commentslink" title="Add Comment">(<?php echo $jcCount; ?>)</a>
The first line is an image tag that displays an icon from the Fam Fam Fam silk icons collection. The href for the link uses the php parameter $this->item->readmore_link to link to the article, and adds the anchor #comments to link directly to the comments form for the article. Then, I used the $jcCount variable in parentheses to display the comment count as the link to the form.