简体   繁体   中英

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!

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM