Keyword

How to Add Prefetch Image Link of K2 Item Image Using Template Override

  • Dayyal Dg.
  • Dayyal Dg.'s Avatar Topic Author
  • Offline
  • New Member
More
2 weeks 5 days ago #178044 by Dayyal Dg.
Hello,

I want to inject a prefetch image link into the header for webpage speed optimization.
Eg:

<link href="example.com/example.jpg" rel="prefetch" as="image" />

I have tried the below code into item view using template override:

<?php
defined('_JEXEC') or die;
// Prefetch Article Image
$doc = JFactory::getDocument();
$doc->addHeadLink('<?php echo $this->item->image; ?>', 'prefetch', 'rel', array('as' => 'image'));
?>

But the output is:
<link href="/<?php echo $this->item->image; ?>" rel="prefetch" as="image" />

How can I get the item image URL?

Please Log in or Create an account to join the conversation.

  • Dayyal Dg.
  • Dayyal Dg.'s Avatar Topic Author
  • Offline
  • New Member
More
2 weeks 5 days ago #178045 by Dayyal Dg.
I have added the below code into item view using template override, and it works.

<?php
defined('_JEXEC') or die;
if($this->item->params->get('itemImage') && !empty($this->item->image)) {
// Prefetch Article Image
$doc = JFactory::getDocument();
$prefetchImage = $this->item->image;
$doc->addHeadLink("$prefetchImage", 'prefetch', 'rel', array('as' => 'image'));
}
?>

Please Log in or Create an account to join the conversation.

  • Fotis
  • Fotis's Avatar
  • Online
  • Administrator
  • JoomlaWorks Support Team
More
2 weeks 2 days ago #178063 by Fotis
There is really no performance benefit in that...

It's best to use a loading="lazy" attribute on the img element if the image is out of view when the item page loads.

If you use & love K2, please take a moment to add your review and rate it
at the Joomla Extensions Directory: extensions.joomla.org/extension/k2/


IMPORTANT: Please search the forum before posting a question!

JoomlaWorks Support Team Member

Please Log in or Create an account to join the conversation.


Powered by Kunena Forum