Keyword

[SOLVED] Custom ALT text in extra field images

More
9 years 1 month ago #142003 by Tim
Currently an image extra field gets the ALT text from the field label. I'm trying to find a way to customise the ALT text - either taking the item title or potentially the value of another extra field called "ALT Text".

This article (getk2.org/documentation/tips-a-tricks/1129-extra-field-image-type-as-link) about converting the output into a hyperlink gets me close, but I can't quite get it to work for this purpose.

Currently my code to insert the field is just
<?php echo $this->item->extraFields->GALLERYIMAGE->value; ?>
in my item.php override

Which results in:
<img alt="Gallery Image" src="/images/gallery/basketball_profile.jpg">

Thanks in advance for any help.

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

  • Kannan Naidu Venugopal
  • Kannan Naidu Venugopal's Avatar
  • Offline
  • Platinum Member
  • Aham Brahmasmi
More
9 years 1 month ago #142004 by Kannan Naidu Venugopal
Replied by Kannan Naidu Venugopal on topic Custom ALT text in extra field images
You can try this..
<?php
        $string = $this->item->extraFields->GALLERYIMAGE->value;
        preg_match('/<img(.*)src(.*)=(.*)"(.*)"/U', $string, $result);
        $imgsrc = array_pop($result);
        ?> 
<img src="<?php echo $imgsrc ?>" alt="<?php echo $this->item->title; ?>" />

It will use the item title as alt

K2 Rocks \m/

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

More
9 years 1 month ago #142067 by Tim
Replied by Tim on topic Custom ALT text in extra field images
That's a great solution Kannan, and easily interchangeable for inserting another extra field as the ALT text instead. It also works fine in a module override with the simple switch of
<?php
        $string = $this->item->extraFields->GALLERYIMAGE->value;
        preg_match('/<img(.*)src(.*)=(.*)"(.*)"/U', $string, $result);
        $imgsrc = array_pop($result);
        ?> 
<img src="<?php echo $imgsrc ?>" alt="<?php echo $this->item->title; ?>" />
with
<?php
	$string = $item->extraFields->GALLERYIMAGE->value;
	preg_match('/<img(.*)src(.*)=(.*)"(.*)"/U', $string, $result);
	$imgsrc = array_pop($result);
	?>
	<img src="<?php echo $imgsrc ?>" alt="<?php echo $item->title; ?>" />
Many thanks - much appreciated!!

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

  • Kannan Naidu Venugopal
  • Kannan Naidu Venugopal's Avatar
  • Offline
  • Platinum Member
  • Aham Brahmasmi
More
9 years 1 month ago #142175 by Kannan Naidu Venugopal
Replied by Kannan Naidu Venugopal on topic Custom ALT text in extra field images
You're welcome. Glad it worked :)

K2 Rocks \m/

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


Powered by Kunena Forum