28 Feb 2019

Get the URL of a K2 extra field image

K2 extra fields can be a royal pain sometimes. They are not nearly as flexible as they should be. For instance, using the image extra field spits renders the image inside it's <img src /> tags. But what if you just want the URL of the image? Perhaps you need the image URL to use inside a CSS background image. Here's how:

Note that the alias of the extra field in my example is myImage. Change this to match your own custom field alias.
Also note that I am using this code inside a mod_k2_content module.

$fetchSrc1 = explode('src="', $item->extraFields->myImage->value);
$fetchSrc2 = explode('"', $fetchSrc1[1]);

<?php echo $fetchSrc2[0]; ?>

For using on K2 item pages, replace the first line with the following:

= explode('src="', $this->item->extraFields->myImage->value);