phpjoomlacommentsjoomla3.0

How to output the amount of comments left by a user with JComments / Joomla 3?


JoomlaTune - developers of the JComments extension - offers the following code to display comments anywhere:

    <?php
            $comments = JPATH_SITE . '/components/com_jcomments/jcomments.php';
                if (file_exists($comments)) {
                require_once($comments);
                $options = array();
                $options['object_id'] = $this->item->id;
                $options['object_group'] = 'com_content';
                $options['published'] = 1;
                $count = JCommentsModel::getCommentsCount($options);
                echo ('<span>'. $count .'</span>');
            }
    ?>

by substituting the id of the article into $this->item->id you can get the number of materials for this article.

Is it possible to somehow adapt this code to display the number of comments left by specific users by his id. Or maybe this variable already exists somewhere in the component code?

Thanks a lot in advance!


Solution

  • all you need to do is add the following code:

    use Joomla\CMS\Factory;
    $user = Factory::getUser();
    $options['userid'] = $user->id;
    

    just before:

    $count = JCommentsModel::getCommentsCount($options);