Thursday, August 26, 2010

k2 how to show comments on latestitem or category

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.

     //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'); ?>
     <?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 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; ?>
     <?php else: ?>
     <?php echo $comment->userName; ?>
     <?php endif; ?>
    <p><?php echo $comment->commentText; ?></p>
     <div class="clr"></div>
   <?php endforeach; ?>
  <?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; ?>

  <?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!

Stumble Upon CodePyro


Asiya said...

THANK YOU VERY MUCH for this solution! yor're great!

Molly said...

Hi Josh,
THANK YOU SOOO MUCH for this awesome article, I was searching a long time to put comments on the K2 category view. It works fine, just 2 little Notices I could not solve, even trying hard, they make me sleepless:
1. Notice: Undefined property: stdClass::$K2CommentsBlock in ~/mytemplate/html/com_k2/category_item.php on line 309.
2. Notice: Undefined property: stdClass::$userImage in ~/mytemplate/html/com_k2/category_item.php on line 331.
I'm a noob girl on K2 and PHP, so please can you give me a jump start on that?

Molly said...

Hi Josh,
with lots of coffee i found the solution good enough for my application:
1. For the $K2CommentsBlock undefind i just erased the whole if- Block from line 11 to 14
2. to get the avatar userImage i added a line just bevor line 34
userImage = K2HelperUtilities::getAvatar($comment->userID, $comment->commentEmail, $this->item->params->get('commenterImgWidth')); ?>

and voila now it is like i wanted it, comments with avatar image on the category view. Thanks again for your good work, wihtout that i wold have faild.

belita said...

Panerai has four above curve of watches, Historic, Contemporary, Manifattura and Appropriate Models. Most watches are created central a bound run replica watches sale and backpack a botheration amount about the case back. Panerai issues Appropriate Models anniversary year. In The Year 2006 it appear the 1936 California Dial Radiomir omega replica absolute edition, a copy from the aboriginal Panerai archetypal anytime accustomed to the Italian Marina Militare. This archetypal was belted to a run of 1936 models. Swiss Panerai Replica makes replica watches uk abounding watches as either bound or appropriate models, advisedly creating beneath watches compared to bazaar demands to be able to advance swiss replica watches a account of exclusivity, which enables it to command a college cost. Merchants may get a brace of bound copy pieces anniversary year and you will acquisition diffuse cat-and-mouse lists for accepted models.

asbdio said...

Dear! It's my great honor to be on your blog,I benefit a lot from reading your blog.
This ubuntu data recovery software also give me help .I want everybody to know it.

Anonymous said...

I like this blog! In return, I want to share some cheap ray ban sunglasses with you.

replicahorlogesnederland said...

El dial replica relojes tiene un color dorado realmente pegadizo y las piedras están realmente bien colocadas también. Bisel se ve bien y el cristal a prueba de rasguños se coloca justo en el caso (detalle muy importante). Movimiento automático japonés de auto-liquidación mantiene el tiempo bien, y también hay una etiqueta verde de holograma replicas rolex en la parte posterior con un número de serie de Rolex. Es bastante llamativo pero todavía en el lado con clase.

replicahorlogesnederland said...

Yup, het is dat bezel die totaal brengt de vintage gevoel in dit stuk
replica rolex. Deze bijzondere stuk heeft al een meer klassieke en sportieve vintage gevoel dan de meeste andere nep-Rolex horloges. Het is gewoon het gevoel dat het wordt me als ik kijk replica horloges naar het. Zwarte wijzerplaat is schoon en het heeft alle goede markeringen, markeringen en handen.
De GMT-functie werkt net als op het origineel en je kunt het instellen van de eerste kroon positie. Draai de kroon tegen de klok en de GMT de hand te verplaatsen. Zet hem met de klok mee en de datum imitatie horloges rolex in te stellen. Japanse automatische beweging veegt de secondewijzer mooi dus het is allemaal goed en solide vanuit dit oogpunt.

replicahorlogesnederland said...

Dial marrón chocolate con los números negros se ve muy bien. El logotipo y las marcas de replicas relojes espana Rolex están bien hechos y los cronógrafos de oro rosa están marcados correctamente también. Las manos brillan muy bien y el bisel se ve muy cerca de la original. Por supuesto que el reloj genuino tendrá ese resplandor 25k y brillo un poco más que éste, pero aún así para las personas regulares como
replicas rolex yo y usted esta es una representación muy cercana de la verdadera oferta.

Anonymous said...

Je pense à passer en revue toutes les replique montre que j'ai revues sur mon blog et rafraîchissant certains de mes commentaires préférés parce que je pense qu'un bon rappel aidera certains de mes lecteurs plus anciens et cohérents et donnera aussi mes lecteurs plus récents un bon matériel de lecture . Voir comment vous les gars comme ce genre d'articles et puis décidera si je devrais les faire plus régulièrement.

Furniture Installation Oklahoma said...

Very nice postt

Post a Comment