[Solved] Get number of comments?

This forum is for general questions about extensions for Joomla! 3.x.

Moderators: pe7er, General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Post Reply
CDXXonline
Joomla! Intern
Joomla! Intern
Posts: 88
Joined: Fri Oct 13, 2017 8:00 pm

[Solved] Get number of comments?

Post by CDXXonline » Sun Jun 24, 2018 3:37 pm

What code would i use to show the number of comments any particular article has? Using jcomments if that matters. Also need code to get last comment posted if anyone knows how to do that.
Last edited by imanickam on Tue Jun 26, 2018 2:24 am, edited 1 time in total.
Reason: Marked the topic as Solved

CDXXonline
Joomla! Intern
Joomla! Intern
Posts: 88
Joined: Fri Oct 13, 2017 8:00 pm

Re: Get number of comments?

Post by CDXXonline » Mon Jun 25, 2018 3:11 am

I found the code i needed, the only problem is how to put it in the WYSIWYG editor? Keeps stripping out php.

CDXXonline
Joomla! Intern
Joomla! Intern
Posts: 88
Joined: Fri Oct 13, 2017 8:00 pm

Re: Get number of comments?

Post by CDXXonline » Mon Jun 25, 2018 7:10 am

Sorry for the multiple posts, won't let me edit after a period of time goes by.

This is what the developer says:
How to display the comments quantity

If you would like to display somewhere the quantity of comments for any object (for example, to display the quantity on an object's intro page), you must include the main JComments file and call static method JComments::getCommentsCount through the function include/require. This method uses two parameters: object's ID and component name.

Code: Select all

$comments = JPATH_SITE . DS .'components' . DS . 'com_jcomments' . DS . 'jcomments.php';
  if (file_exists($comments)) {
    require_once($comments);
    $count = JComments::getCommentsCount($id, 'com_mycomp');
    echo $count ? ('Comments('. $count . ')') : 'Add comment';
  }
where $id — is the commented object ID and com_mycomp — component name. This code displays the 'Comments (5)' if you have 5 comments of given object and 'Add comments' if there are no comments.
But what is main jcomments file? How to call static method? There is no other information other than what is listed. A lot of this is like here's some code, now go spend 4 days trying figure out where to put it, whether multiple files need to be created or edited and what goes around it. What about how to add it to articles when the editor strips all php? Is there some little pice of information that might be relevant to know to put in there?

User avatar
starazure
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 142
Joined: Mon Sep 11, 2017 5:18 pm
Location: OH, United States
Contact:

Re: Get number of comments?

Post by starazure » Mon Jun 25, 2018 2:59 pm

So where are you trying to show the number of comments ? Is it at the top of the article ? What template are you using ?
Free fastest Joomla themes - https://starazure.com/joomla-templates

Free useful Joomla extensions - https://starazure.com/extensions

CDXXonline
Joomla! Intern
Joomla! Intern
Posts: 88
Joined: Fri Oct 13, 2017 8:00 pm

Re: Get number of comments?

Post by CDXXonline » Mon Jun 25, 2018 4:18 pm

Inside a custom module. I had to make a custom module to display more than one menu item, I want the number of comments to show up on the page after their article name. I couldn't put the code above in the wysiwyg editor in any case, so i built a custom module. Now i just need to figure out where to put everything. If i replace the hello world greeting from the module build with the html code i copied from the editor it shows up fine, but there are 20 or 30 spots i need to stick the code above to show the number of comments in each article.

Can't seem to leave this

Code: Select all

public static function getBoards($params)
    {
        return '       ';
    }
area. I have the html code in between the ' ', and i've tried every combination to try and leave the html to put in the php code that needs put throughout for the number of comments.

I had the code working on the tpl_comment file, so i know it shows what i want it to show, now just can't get the helper.php file to accept the code.

Here's the page of the site i'm working on:

https://www.cdxx-us.com/index.php/general-boards

Where all those 0s are is where the code needs to go. The id's from the jcomment quantity code will be pointing to those specific article numbers, so each code will have a slight variation. The module is set to only show on that page, and that's the only way to get 5 categories (menu items) to list and show the articles from each category. I haven't even started on trying to get the latest comments to show up yet lol.

User avatar
starazure
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 142
Joined: Mon Sep 11, 2017 5:18 pm
Location: OH, United States
Contact:

Re: Get number of comments?

Post by starazure » Mon Jun 25, 2018 4:55 pm

If you want the number to show right after the name of the article, it could be done with a template override. If you pm me your site where I can modify the template files and this will be the best option for performance..

Alternate option is to use this to add code anywhere including a custom html module.. https://extensions.joomla.org/extension/sourcerer/

I don't like using such extensions and try to keep it fast and simple .. so your site will load fast.
Free fastest Joomla themes - https://starazure.com/joomla-templates

Free useful Joomla extensions - https://starazure.com/extensions

CDXXonline
Joomla! Intern
Joomla! Intern
Posts: 88
Joined: Fri Oct 13, 2017 8:00 pm

Re: Get number of comments?

Post by CDXXonline » Tue Jun 26, 2018 1:35 am

I got this figured out, this can be deleted or whatever.

Stripped the initial code from the module build and used the mod_xxxxxx.php file for content and the helper for the extra code the creator included, which is probably how you're supposed to do it anyway. Now i can put php in. Works on testing, and i can put the last comment in too, so both questions in the op are resolved.

Something they don't tell you at the end of the module building tutorial in joomla docs is after you make it you have to go to extentions > install > discover, then discover and install it to get it to show up on the module types list. I guess that part is a secret. Also they should include where your code is actually supposed to go instead of echoing something from the helper file. mod_xxxxxx.php is for code, helper is for css and whatever extra stuff you want to call into the mod file.

Just wanted to make a comment to close this out.

CDXXonline
Joomla! Intern
Joomla! Intern
Posts: 88
Joined: Fri Oct 13, 2017 8:00 pm

Re: [Solved] Get number of comments?

Post by CDXXonline » Tue Jun 26, 2018 6:59 am

Just a quick note on this in case anyone stumbles onto it and didn't get their latest comments question answered.


The latest comment code from the developer didn't work as good as the get quantities code. Here's what i did:

1. Clone jcomments latest module, because i wanted a different look to the output i was already using sitewide and needed to edit the way it displayed latest comments. If you don't care if it looks the same don't worry about it.

2. Next i had to install the Modules Anywhere extension.

3. Next create a category for every individual article, because jcomments latest module doesn't let you pick articles, just categories for last comments, so if you want the latest for 1 article you need to drop it in a category with only that article, then you get the latest from that article and no others are overriding the one you want displayed.

4. Next put the article/s in the category/ies you created.

5. Next create the module/s you need to display what you're trying to display with your clone jcomments latest.

6. Next go back to the mod_xxxxx.php file and enter {module [name_of_mod_you_created]}

That's it, the latest comment should show up where you put the {module xxxxxx} code.


Post Reply

Return to “Extensions for Joomla! 3.x”