If your here you are wondering how the heck am i able to show the comments section for a K2 item in either the latestitem or category template.
For starters you need to create a customized template override by placing a copy of
components/com_k2/templates into /templates/[YOUR TEMPLATE]/html/com_k2/.
Note: notice there will not be a templates folder in your custom html/com_k2. The template files go directly into the com_k2 folder!
More details on templates
I am using a menu item to pull k2 content, which doesn't show you the menu for selecting a sub-template. Basically this requires you to edit the template in the Default folder.
Now go ahead and edit the /templates/[YOUR TEMPLATE]/html/com_k2/latest_item.php. Find the line that says...
<?php echo $this->item->event->K2AfterDisplay; ?>
Now paste the following code just after that line.
<?php
//you can set the limit start by using pagination; we only needed the top 3 comments
//look at line 143 of /components/com_k2/views/item/view.html.php
$limitstart =0;
$limit = 3;
//get the comments array
$comments = K2ModelItem::getItemComments($this->item->id, $limitstart, $limit);
$this->item->comments = $comments;
?>
<?php if($this->item->params->get('itemComments') && ( ($this->item->params->get('comments') == '2' && !$this->user->guest) || ($this->item->params->get('comments') == '1'))):?>
<!-- K2 Plugins: K2CommentsBlock -->
<?php echo $this->item->event->K2CommentsBlock; ?>
<?php endif;?>
<?php if($this->item->params->get('itemComments') && !JRequest::getInt('print') && ($this->item->params->get('comments') == '1' || ($this->item->params->get('comments') == '2')) && empty($this->item->event->K2CommentsBlock)):?>
<!-- Item comments -->
<a name="itemCommentsAnchor" id="itemCommentsAnchor"></a>
<div class="itemComments">
<?php if($this->item->comments): ?>
<ul class="itemCommentsList">
<?php foreach ($this->item->comments as $key=>$comment): ?>
<li class="<?php echo ($key%2) ? "odd" : "even"; echo (!$this->item->created_by_alias && $comment->userID==$this->item->created_by) ? " authorResponse" : ""; ?>">
<span class="commentLink">
<a href="<?php echo $this->item->link; ?>#comment<?php echo $comment->id; ?>" name="comment<?php echo $comment->id; ?>" id="comment<?php echo $comment->id; ?>">
<?php echo JText::_('Comment Link'); ?>
</a>
</span>
<?php if($comment->userImage):?>
<img src="<?php echo $comment->userImage; ?>" alt="<?php echo $comment->userName; ?>" width="<?php echo $this->item->params->get('commenterImgWidth'); ?>" />
<?php endif; ?>
<span class="commentDate">
<?php echo JHTML::_('date', $comment->commentDate, JText::_('DATE_FORMAT_LC2')); ?>
</span>
<span class="commentAuthorName">
<?php echo JText::_("posted by"); ?>
<?php if(!empty($comment->userLink)): ?>
<a href="<?php echo $comment->userLink; ?>" title="<?php echo $comment->userName; ?>" target="_blank" rel="nofollow">
<?php echo $comment->userName; ?>
</a>
<?php else: ?>
<?php echo $comment->userName; ?>
<?php endif; ?>
</span>
<p><?php echo $comment->commentText; ?></p>
<div class="clr"></div>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?php $user = &JFactory::getUser(); if ($this->item->params->get('comments') == '2' && $user->guest):?>
<!-- <div>
<a href="<?php echo $this->item->link.'#itemCommentsAnchor'; ?>" title="<?php if(!empty($this->item->image_caption)) echo $this->item->image_caption; else echo $this->item->title; ?>">
<?php echo JText::_('Login to post comments');?></a></div>-->
<div class="s5box_login cboxelement"><a href="#"><?php echo JText::_('Login to post comments');?></a></div>
<?php endif; ?>
</div>
<?php endif; ?>
If your interested the magic comes from the call to the K2ModelItem::getItemComments function. It actually retrieves the Comments array since it is not included in the Categories nor latest item. The Comments display code came directly from the item.php in the templates. Take a look at it if you need to add other functionality!

1 comments:
THANK YOU VERY MUCH for this solution! yor're great!
Post a Comment