How to get Joomla K2 item as object on category pa

2019-07-15 12:44发布

问题:

I need to get all the items of specific K2 category as object in templates/mytemplate/html/com_k2/default/category.php. Something like:

foreach($this->category->items as $item) {
 echo $item->image;
}

but I dont' know API of K2 component. To get not only $this->leading or $this->primary or $this->secondary with their limits but ALL the items of the current category

回答1:

You may get it with direct query to MySQL:

$catid = $this->category->id;
$db = &JFactory::getDBO();
$query = $db->getQuery(true);
$query->select($db->quoteName(array('id','title','published','ordering')))
      ->from($db->quoteName('#__k2_items'))
      ->where($db->quoteName('catid')." = ".$db->quote($catid))
      ->order($db->quoteName('ordering').'ASC');
$db->setQuery($query);
$itemList = $db->loadObjectList();
if(count($itemList) > 0) {
foreach ($itemList as $item){
if($item->published == 1) {
 echo '<img src="/media/k2/items/src/'.md5('Image'.$item->id).'.jpg" alt="'.$item->title.'" />';
} // if published
} // foreach
} // if count > 0