Welcome to the new HowToJoomla!

We've completely revamped the design to make the site easier to read and easier to navigate. We hope you like the changes!

How to add a JomComment comment counter to your Joomla!-powered blog

January 20, 2009 | by Cory | 20 Comments

As promised, here is an article about one of the things I learned while upgrading How To Joomla to Joomla! 1.5. I have been using JomComment on How To Joomla for quite a while. You may have noticed that besides having the usual "Add Comment (2)" link at the end of each article introduction, I have also added a comment counter and link along with the "Written by...," date, section, and category, as shown in this image:

JomComment Counter

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.

Step 1: Override the content component output in your template

Without getting too deep into how you override the content component's output in your template, I will try to give a brief overview of this process. 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:

  • article/default.php
  • category/blog_item.php
  • frontpage/default_item.php
  • section/blog_item.php

Step 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.

 
<?php
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.

Step 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. For HowToJoomla, the following code is 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.

Questions/Comments

As always, if you have questions or comments please feel free to post them here.

About the Author

Cory WebbIn the years since Joomla! was founded, Cory has built dozens of websites with Joomla! and helped thousands of people find answers to questions about Joomla! through HowToJoomla.net. Cory has also written a book about Joomla titled Beginning Joomla! Web Site Development published by Wrox in April, 2009. In February of 2008, Cory founded Cory Webb Media, LLC, where he provides consulting and web development services for companies of all sizes. You can follow Cory on Twitter @corywebb, or become a fan of Cory Webb Media on Facebook.

Read More

Trackback(0)

Comments (20)Add Comment

0
Author & Date in 1 line Joomla
written by greg, January 20, 2009
How to add Author & data in 1 line joomla cms? When i'm modify this in joomla 1.5.9?
62
...
written by Cory, January 20, 2009
Hi greg,

You can do that by modifying the 4 files listed in this article:

* article/default.php
* category/blog_item.php
* frontpage/default_item.php
* section/blog_item.php

I will try to add further explanation in another article.
244
...
written by Jason Boyette, January 23, 2009
Thanks for this tutorial. I use JomComment as well, but don't really like how the links to comments and hits are displayed by default. I'll look forward to further explanations on how to change the appearance.
0
thanks
written by rapidshare search, February 23, 2009
thank you very much for this tutorial
0
Great "how to"!
written by John, March 19, 2009
I have it up and running on my test site. The "add comments" text and the number of comments appears correctly but the link to the comments box isn't working. Any suggestions?

Any help appreciated.
thanks

John
62
...
written by Cory, March 20, 2009
Hi John,

I'm not sure what to tell you. Using "$this->item->readmore_link" should work.

Cory
0
supercars
written by word, April 11, 2009
0
not in article.php
written by Steve, April 19, 2009
Hello,
this add-on doesn't work in article.php
If you look at your counter close to date date has "0" comment, but this post has 8 comments.
I tested and works perfect in all others file but not in article.php
Any suggestions (will be great!)?
thanks for attention
Stefano
0
yes
written by Steve, April 19, 2009
Hello again,
i found the solution!
in * article/default.php use

$jcCount = jcCountComment($this->article->id);

instead of
$jcCount = jcCountComment($this->item->id);

i'm happy smilies/cheesy.gif
lol, Lowly rated comment [Show]
0
thanks alot
written by Denali, May 09, 2009
Thanks for finding the solution for article/default.php. smilies/wink.gif
0
...
written by betclic, May 23, 2009
So usefull, you have a good solution for this problem...
0
Thank You
written by Baile Herculane, June 11, 2009
Thank You for resove...
916
Comment Counter
written by elango, July 17, 2009
It works fine, i added a sample comment; i got the message added confirmation and what i added was appended with that article. so far so good; but when i revisited that article the comment was not there; aand the counter showed '0'. Pls i need a solution right now, thanks in advance.
0
solution for john
written by Madridista, July 20, 2009
Hi John,

Try to unpublish the "Jom Comment Sys" plugin.
It worked for me.

Hope it can help
0
Vote counter
written by Davd, July 29, 2009
Hi,
Instead of the comment counter, this question is related to vote counter. Currently, there is no sense of how many votes occur, only the difference between the positive and the negative votes (like +0, -2, etc.). Does anyone know of a way to display the actual vote counters next to or below the "+" or "-" symbols?
Thanks.
0
Alwin123
written by Alwin123, August 19, 2009
I am using the Jomcomment with the Jomsocial. Now I have show all the comments and the comment form in anther page , which can be accessed by clicking the number of comments (displayed in the brackets using this hack)..It wil be helpful if anyone can figure out what has to be done..?
0
top ten security problems with joomla
written by malibu rehab, September 16, 2009
Make sure you have regular Joomla backups. In case your site gets hacked or something happens, you will be able to rebuild from scratch.
0
I love you
written by Mazgalici, September 21, 2009
I love you dude, you saved my ass of a lot of work smilies/grin.gif
1523
...
written by wannabe, January 31, 2010
where do i add the code in step 3? and how do i get the plus/minus thingy at the bottom of my jcomments like you have ? smilies/smiley.gif

Write comment

You must be logged in to post a comment. Please register if you do not have an account yet.

busy