По умолчанию К2 выводит только следующий и предыдущий материал в этой категории. Для интернет магазина понадобилось вывести 4 товара из одной категории.
Решение:
1.Создаем массив данных
public_html/components/com_k2/views/item/view.html.php
Добавляем новые данные в общий массив, строка 302
// Navigation (previous and next item)
if ($item->params->get('itemNavigation'))
{
$model = $this->getModel('item');
$nextItem = $model->getNextItem($item->id, $item->catid, $item->ordering);
if (!is_null($nextItem))
{
$item->nextLink = urldecode(JRoute::_(K2HelperRoute::getItemRoute($nextItem->id.':'.urlencode($nextItem->alias), $nextItem->catid.':'.urlencode($item->category->alias))));
$item->nextTitle = $nextItem->title;
$date = JFactory::getDate($item->modified);
$timestamp = '?t='.$date->toUnix();
if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$nextItem->id).'_XS.jpg'))
{
$item->nextImageXSmall = JURI::base(true).'/media/k2/items/cache/'.md5("Image".$nextItem->id).'_XS.jpg'.$timestamp;
}
if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$nextItem->id).'_S.jpg'))
{
$item->nextImageSmall = JURI::base(true).'/media/k2/items/cache/'.md5("Image".$nextItem->id).'_S.jpg'.$timestamp;
}
if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$nextItem->id).'_M.jpg'))
{
$item->nextImageMedium = JURI::base(true).'/media/k2/items/cache/'.md5("Image".$nextItem->id).'_M.jpg'.$timestamp;
}
if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$nextItem->id).'_L.jpg'))
{
$item->nextImageLarge = JURI::base(true).'/media/k2/items/cache/'.md5("Image".$nextItem->id).'_L.jpg'.$timestamp;
}
if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$nextItem->id).'_XL.jpg'))
{
$item->nextImageXLarge = JURI::base(true).'/media/k2/items/cache/'.md5("Image".$nextItem->id).'_XL.jpg'.$timestamp;
}
if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$nextItem->id).'_Generic.jpg'))
{
$item->nextImageGeneric = JURI::base(true).'/media/k2/items/cache/'.md5("Image".$nextItem->id).'_Generic.jpg'.$timestamp;
}
}
//+1
if (!is_null($nextItem)){
$nextItem_p1 = $model->getNextItem($nextItem->id, $nextItem->catid, $item->ordering);
if (!is_null($nextItem_p1))
{
$item->nextLink_p1 = urldecode(JRoute::_(K2HelperRoute::getItemRoute($nextItem_p1->id.':'.urlencode($nextItem_p1->alias), $nextItem_p1->catid.':'.urlencode($item->category->alias))));
$item->nextTitle_p1 = $nextItem_p1->title;
$date = JFactory::getDate($item->modified);
$timestamp = '?t='.$date->toUnix();
if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$nextItem_p1->id).'_XS.jpg'))
{
$item->nextImageXSmall_p1 = JURI::base(true).'/media/k2/items/cache/'.md5("Image".$nextItem_p1->id).'_XS.jpg'.$timestamp;
}
if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$nextItem_p1->id).'_S.jpg'))
{
$item->nextImageSmall_p1 = JURI::base(true).'/media/k2/items/cache/'.md5("Image".$nextItem_p1->id).'_S.jpg'.$timestamp;
}
if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$nextItem_p1->id).'_M.jpg'))
{
$item->nextImageMedium_p1 = JURI::base(true).'/media/k2/items/cache/'.md5("Image".$nextItem_p1->id).'_M.jpg'.$timestamp;
}
if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$nextItem_p1->id).'_L.jpg'))
{
$item->nextImageLarge_p1 = JURI::base(true).'/media/k2/items/cache/'.md5("Image".$nextItem_p1->id).'_L.jpg'.$timestamp;
}
if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$nextItem_p1->id).'_XL.jpg'))
{
$item->nextImageXLarge_p1 = JURI::base(true).'/media/k2/items/cache/'.md5("Image".$nextItem_p1->id).'_XL.jpg'.$timestamp;
}
{
$item->nextImageGeneric_p1 = JURI::base(true).'/media/k2/items/cache/'.md5("Image".$nextItem_p1->id).'_Generic.jpg'.$timestamp;
}
}}
$previousItem = $model->getPreviousItem($item->id, $item->catid, $item->ordering);
if (!is_null($previousItem))
{
$item->previousLink = urldecode(JRoute::_(K2HelperRoute::getItemRoute($previousItem->id.':'.urlencode($previousItem->alias), $previousItem->catid.':'.urlencode($item->category->alias))));
$item->previousTitle = $previousItem->title;
$date = JFactory::getDate($item->modified);
$timestamp = '?t='.$date->toUnix();
if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$previousItem->id).'_XS.jpg'))
{
$item->previousImageXSmall = JURI::base(true).'/media/k2/items/cache/'.md5("Image".$previousItem->id).'_XS.jpg'.$timestamp;
}
if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$previousItem->id).'_S.jpg'))
{
$item->previousImageSmall = JURI::base(true).'/media/k2/items/cache/'.md5("Image".$previousItem->id).'_S.jpg'.$timestamp;
}
if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$previousItem->id).'_M.jpg'))
{
$item->previousImageMedium = JURI::base(true).'/media/k2/items/cache/'.md5("Image".$previousItem->id).'_M.jpg'.$timestamp;
}
if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$previousItem->id).'_L.jpg'))
{
$item->previousImageLarge = JURI::base(true).'/media/k2/items/cache/'.md5("Image".$previousItem->id).'_L.jpg'.$timestamp;
}
{
$item->previousImageXLarge = JURI::base(true).'/media/k2/items/cache/'.md5("Image".$previousItem->id).'_XL.jpg'.$timestamp;
}
if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$previousItem->id).'_Generic.jpg'))
{
$item->previousImageGeneric = JURI::base(true).'/media/k2/items/cache/'.md5("Image".$previousItem->id).'_Generic.jpg'.$timestamp;
}
}
//+1
if (!is_null($previousItem))
{
$previousItem_p1 = $model->getPreviousItem($previousItem->id, $previousItem->catid, $item->ordering);
if (!is_null($previousItem_p1))
{
$item->previousLink_p1 = urldecode(JRoute::_(K2HelperRoute::getItemRoute($previousItem_p1->id.':'.urlencode($previousItem_p1->alias), $previousItem->catid.':'.urlencode($item->category->alias))));
$item->previousTitle_p1 = $previousItem_p1->title;
$date = JFactory::getDate($item->modified);
$timestamp = '?t='.$date->toUnix();
if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$previousItem_p1->id).'_XS.jpg'))
{
$item->previousImageXSmall_p1 = JURI::base(true).'/media/k2/items/cache/'.md5("Image".$previousItem_p1->id).'_XS.jpg'.$timestamp;
}
if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$previousItem_p1->id).'_S.jpg'))
{
$item->previousImageSmall_p1 = JURI::base(true).'/media/k2/items/cache/'.md5("Image".$previousItem_p1->id).'_S.jpg'.$timestamp;
}
if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$previousItem_p1->id).'_M.jpg'))
{
$item->previousImageMedium_p1 = JURI::base(true).'/media/k2/items/cache/'.md5("Image".$previousItem_p1->id).'_M.jpg'.$timestamp;
}
if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$previousItem_p1->id).'_L.jpg'))
{
$item->previousImageLarge_p1 = JURI::base(true).'/media/k2/items/cache/'.md5("Image".$previousItem_p1->id).'_L.jpg'.$timestamp;
}
if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$previousItem_p1->id).'_XL.jpg'))
{
$item->previousImageXLarge_p1 = JURI::base(true).'/media/k2/items/cache/'.md5("Image".$previousItem_p1->id).'_XL.jpg'.$timestamp;
}
if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$previousItem_p1->id).'_Generic.jpg'))
{
$item->previousImageGeneric_p1 = JURI::base(true).'/media/k2/items/cache/'.md5("Image".$previousItem_p1->id).'_Generic.jpg'.$timestamp;
}
}
}
2.Редактируем шаблон
public_html/components/com_k2/templates/default/item.php
В данном примере выводим список с названием и фотографией. заменяем блок <!-- Item navigation -->
<!-- Item navigation -->
<div class="itemNavigation">
<h3 class="itemNavigationTitle"><?php echo JText::_('K2_MORE_IN_THIS_CATEGORY'); ?></h3>
<ul style="padding-left: 0px;display: flex;">
<?php if(isset($this->item->previousLink)): ?>
<li class="even">
<div id="itemRelTitle">
<a class="itemRelTitle" href="/<?php echo $this->item->previousLink; ?>"><?php echo $this->item->previousTitle; ?></a>
</div>
<a class="itemRelTitle" href="/<?php echo $this->item->previousLink; ?>">
<img style="width:100px;height:auto;" class="itemRelImg" src="/<?php echo $this->item->previousImageMedium; ?>" alt="">
</a>
</li>
<?php endif; ?>
<?php if(isset($this->item->previousLink_p1)): ?>
<li class="even">
<div id="itemRelTitle">
<a class="itemRelTitle" href="/<?php echo $this->item->previousLink_p1; ?>"><?php echo $this->item->previousTitle_p1; ?></a>
</div>
<a class="itemRelTitle" href="/<?php echo $this->item->previousLink_p1; ?>">
<img style="width:100px;height:auto;" class="itemRelImg" src="/<?php echo $this->item->previousImageMedium_p1; ?>" alt="">
</a>
</li>
<?php endif; ?>
<?php if(isset($this->item->nextLink)): ?>
<li class="odd">
<div id="itemRelTitle">
<a class="itemRelTitle" href="/<?php echo $this->item->nextLink; ?>"><?php echo $this->item->nextTitle; ?></a>
</div>
<a class="itemRelTitle" href="/<?php echo $this->item->nextLink; ?>">
<img style="width:100px;height:auto;" class="itemRelImg" src="/<?php echo $this->item->nextImageMedium; ?>" alt="">
</a>
</li>
<?php endif; ?>
<?php if(isset($this->item->nextLink_p1)): ?>
<li class="odd">
<div id="itemRelTitle">
<a class="itemRelTitle" href="/<?php echo $this->item->nextLink_p1; ?>"><?php echo $this->item->nextTitle_p1; ?></a>
</div>
<a class="itemRelTitle" href="/<?php echo $this->item->nextLink_p1; ?>">
<img style="width:100px;height:auto;" class="itemRelImg" src="/<?php echo $this->item->nextImageMedium_p1; ?>" alt="">
</a>
</li>
<?php endif; ?>
</ul>
</div>
Результат :