This is more of a PHP question but it is also related to Joomla. In K2's category.php file, there are these lines:

    <?php foreach($this->links as $key=>$item): ?>

    <div class="itemContainer">
            // Load category_item.php by default
            $this->item = $item;
            echo $this->loadTemplate('item');

    <?php endforeach; ?>

which iterates through items in a category and load template(category_item.php) for each one of them.

Now in the category_item.php file, I wanna get information from another item (for example, in item 2, I wanna display image of item 3, sounds weird but it is what I want to do). In category_item.php,this is how image gets displayed:

<img src="<?php echo $this->item->image; ?>">

the $this here refers to the current item, so my question is, from within the category_item.php file, is there anyway to refer to the category object? And is there anyway to refer to other items?

2 Answers 2


We have category.php and item.php files. category.php load template in loop like:

foreach($this->links as $key => $item) :
    $this->item = $item;

So in item.php file you have access or you should have to $this and every property you have created in JView.

You wanna access the next property of loop, so you need only to pass a $key to item.php

foreach($this->links as $key => $item) :
    $this->item = $item;
    $this->key = $key;

Then in your item.php you can access next value of item like so:

$this->links[$this->key + 1]->image 

or other property of next, previous or what $key you wanna to.


Well, although a little bit vague what you want to do, you could achieve something like this by creating an override in the category.php file and bypass the default item.php template loading, so you can easily switch back to another element of the category items array...

A very simplified example would be:

$i = 0;
    foreach ($this->leading as $key=>$item): ?>
        <div class="item">
        $this->item = $item;
        echo '<h2>'. $this->item->title .'</h2>';
        if ($i == 1)
            $this->item = $this->leading[$i-1];
            echo '<h3>'.$this->item->title.'</h3>';
            echo '<img src="'. $this->item->imageMedium . '" />'; 
<?php endforeach;
  • It's like a rape on a joomla ; ) You need to pass reference of parent $category aka $this to child template.
    – Lanah
    Apr 28, 2016 at 5:49
  • @Lanah - sort of rape someone can say it, yes - as it skips the whole item template. When writing this, I wanted first to present that what it takes is to access previous elements of the $category... then not sure how I stacked with the silly impression that $category won't pass into the item template, I didn't even give it a thought... At the end both answers suggest the same thing - but mine is dirty/ugly as hell and looks like an amateurish one. So, I upvoted yours and I let mine here at the bottom as an example of blurred thinking coding.
    – FFrewin
    May 2, 2016 at 11:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.