Monday , February 3 2025

How to display another image preview for the particular language version

I need to use another image for the K2 article for the another lang. For example, we have 2 languages used for the site: en (by default) and additional pl. In the administrator UI we can edit only general image preview. I made some edits to use another image for the 2nd lang. First of all, we need to create extra field with the name Additionalimagepl. Then we can open template file (e.g., /templates/template/html/com_k2/news/item.php), find this part:

<a class="modal" rel="{handler: 'image'}" href="<?php echo $this->item->imageXLarge; ?>" title="<?php echo JText::_('K2_CLICK_TO_PREVIEW_IMAGE'); ?>">
<img src="<?php echo $this->item->image; ?>" alt="<?php if(!empty($this->item->image_caption)) echo K2HelperUtilities::cleanHtml($this->item->image_caption); else echo K2HelperUtilities::cleanHtml($this->item->title); ?>" />
</a>

and edit this code in the following way:

<?php if(strpos($_SERVER['REQUEST_URI'], '/pl/') !== false):?>
<?php if($this->item->extraFields->Additionalimagepl->value != ''): ?>
<?php
$varimg = $this->item->extraFields->Additionalimagepl->value ;    
$varimg = preg_replace('/<img src="/',"",$varimg);
$varimg = preg_replace('/" alt="" \>/'," ",$varimg);
echo '<a class="modal" href="' . $varimg . "" . "" ;
?>
<?php echo $this->item->extraFields->Additionalimagepl->value; ?>
</a>
<?php else: ?>
<a class="modal" rel="{handler: 'image'}" href="<?php echo $this->item->imageXLarge; ?>" title="<?php echo JText::_('K2_CLICK_TO_PREVIEW_IMAGE'); ?>">
<img src="<?php echo $this->item->image; ?>" alt="<?php if(!empty($this->item->image_caption)) echo K2HelperUtilities::cleanHtml($this->item->image_caption); else echo K2HelperUtilities::cleanHtml($this->item->title); ?>" />
</a>
<?php endif; ?>
<?php else: ?>
<a class="modal" rel="{handler: 'image'}" href="<?php echo $this->item->imageXLarge; ?>" title="<?php echo JText::_('K2_CLICK_TO_PREVIEW_IMAGE'); ?>">
<img src="<?php echo $this->item->image; ?>" alt="<?php if(!empty($this->item->image_caption)) echo K2HelperUtilities::cleanHtml($this->item->image_caption); else echo K2HelperUtilities::cleanHtml($this->item->title); ?>" />
</a>
<?php endif; ?>

About iryna

I'm Iryna, a web developer from Ukraine with a decade of experience solving complex technical challenges in the world of freelance. Throughout my career, I've worked on everything from troubleshooting server-side issues and optimizing website performance to enhancing user interfaces. On this blog, I share detailed solutions to the technical problems I’ve encountered and methods that have worked best for me. In addition to my technical expertise, I’m also passionate about digital drawing. I hope the tutorials and insights I provide here will help both fellow developers and creatives alike in their own projects.

Check Also

Find anchor in URL and scroll to its id on this page using JS

Here is a case: if a link contains a word, then direct the user to …

Leave a Reply

Your email address will not be published. Required fields are marked *