Forum
Welcome, Guest
Please Login to access forum.
Re:Include comments count into featured articles (1 viewing) 
Go to bottom
TOPIC: Re:Include comments count into featured articles
#8092
John Dagelmore
Admin
Posts: 3731
User Offline
Include comments count into featured articles Karma: 79  
It's possible to show the comments count for example in the 'featured' view using a template override for the file 'templates/mytemplate/html/com_content/featured/default_item.php' and placing the following code for Joomla 3 at the bottom:

Code:

require_once(JPATH_ROOT . '/components/com_jcomment/classes/factory.php'); require_once(JPATH_ROOT . '/components/com_jcomment/classes/multilingual.php'); require_once(JPATH_ROOT . '/components/com_jcomment/models/jcomment.php'); $count = 0; $options = array(); $options['object_id'] = (int)$this->item->id; $options['object_group'] = 'com_content'; $options['published'] = 1; JFactory::getApplication()->input->set ( 'view', 'article' ); $count = JCommentModel::getCommentsCount($options); JFactory::getApplication()->input->set ( 'view', 'featured' ); $link = JRoute::_(ContentHelperRoute::getArticleRoute($this->item->slug, $this->item->catid, $this->item->language)) . '#addcomments'; echo '<a href="' . $link . '"><span class="icon-envelope"></span> ' . $count . ' comments - Add comment</a>';
 
Logged Logged  
  The administrator has disabled public write access.
#8142
John Dagelmore
Admin
Posts: 3731
User Offline
Re:Include comments count into featured articles Karma: 79  
The code for Joomla 5 and Joomla 4 is the following one:

Code:

require_once (JPATH_ROOT . '/components/com_jcomment/classes/factory.php'); require_once (JPATH_ROOT . '/components/com_jcomment/classes/multilingual.php'); require_once (JPATH_ROOT . '/components/com_jcomment/models/jcomment.php'); $count = 0; $options = array (); $options ['object_id'] = ( int ) $this->item->id; $options ['object_group'] = 'com_content'; $options ['published'] = 1; Factory::getApplication ()->input->set ( 'view', 'article' ); $count = JCommentModel::getCommentsCount ( $options ); Factory::getApplication ()->input->set ( 'view', 'featured' ); $link = Route::_ ( Joomla\Component\Content\Site\Helper\RouteHelper::getArticleRoute ( $this->item->slug, $this->item->catid, $this->item->language ) ) . '#addcomments'; echo '<a href="' . $link . '"><span class="icon-envelope"></span> ' . $count . ' comments - Add comment</a>';
 
Logged Logged  
  The administrator has disabled public write access.
#8143
John Dagelmore
Admin
Posts: 3731
User Offline
Re:Include comments count into featured articles Karma: 79  
The code for Joomla a4 and Joomla 5 to allow to post comments only if permissions are allowd is the following:

Code:

require_once (JPATH_ROOT . '/components/com_jcomment/classes/factory.php'); require_once (JPATH_ROOT . '/components/com_jcomment/classes/multilingual.php'); require_once (JPATH_ROOT . '/components/com_jcomment/classes/security.php'); require_once (JPATH_ROOT . '/components/com_jcomment/classes/acl.php'); require_once (JPATH_ROOT . '/components/com_jcomment/models/jcomment.php'); $acl = JCommentFactory::getACL(); if($acl->canComment) { $count = 0; $options = array (); $options ['object_id'] = ( int ) $this->item->id; $options ['object_group'] = 'com_content'; $options ['published'] = 1; Factory::getApplication ()->input->set ( 'view', 'article' ); $count = JCommentModel::getCommentsCount ( $options ); Factory::getApplication ()->input->set ( 'view', 'featured' ); $link = Route::_ ( Joomla\Component\Content\Site\Helper\RouteHelper::getArticleRoute ( $this->item->slug, $this->item->catid, $this->item->language ) ) . '#addcomments'; echo '<a href="' . $link . '"><span class="icon-envelope"></span> ' . $count . ' comments - Add comment</a>'; }
 
Logged Logged  
  The administrator has disabled public write access.
Go to top