- Posts: 13
COMMUNITY FORUM
K2 Extra Fields Image Tag Src Extract
- Design
- Topic Author
- Offline
- New Member
Less
More
9 years 1 month ago #147978
by Design
K2 Extra Fields Image Tag Src Extract was created by Design
Hello! I'm currently struggling in a way that I could use the K2 Image Extra Field as a link that I could myself implement in my site. I need some sort of PHP code that can extract only this part, taking notice I am already implementing the K2 fields into the PHP separately using the:
I found a article by google that shows this:
However it didn't work for me. I think it only works for the For loop, as I have seen by comments.
Does anyone have any idea how I can do this?
OBS: I need this because I need it to work something like this:
Any help?
//convertArray Extra Fields to use ids as key
$extrafields = array();
foreach($this->item->extra_fields as $item)
{
$extrafields[$item->id] = $item->value;
}
//convertArray Extra Fields to use labels as key
$extrafieldslabels = array();
foreach($this->item->extra_fields as $itemlabel)
{
$extrafieldslabels[$itemlabel->id] = $itemlabel->name;
}
I found a article by google that shows this:
<?php
$var = $this->item->extraFields->Image->value ;
$var = preg_replace('/<img src="/',"",$var);
$var = preg_replace('/" alt="Image" \>/'," ",$var);
echo '<a href="' . $var . "TextOfLink" . "</a>" ;
?>
However it didn't work for me. I think it only works for the For loop, as I have seen by comments.
Does anyone have any idea how I can do this?
OBS: I need this because I need it to work something like this:
<div style="background-image: url('<?php echo $extrafields[28]; ?>.png')";>
Any help?
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
- Offline
- Platinum Member
Less
More
- Posts: 15920
9 years 1 month ago #147991
by Krikor Boghossian
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Replied by Krikor Boghossian on topic K2 Extra Fields Image Tag Src Extract
You can use this as a starting point.
github.com/kricore/Advanced-templating-with-K2/blob/master/_inc/cheatsheet.php#L76-L80
github.com/kricore/Advanced-templating-with-K2/blob/master/_inc/cheatsheet.php#L76-L80
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- Design
- Topic Author
- Offline
- New Member
Less
More
- Posts: 13
9 years 1 month ago #148010
by Design
Replied by Design on topic K2 Extra Fields Image Tag Src Extract
Sorry, but this doesn't look like what I'm talking about. This seems like a way of using K2 Extra Fields Image as the Item Image.
And I'm not really a PHP developer. I try to work my way around many times by testing millions of possibilities until it works, but I would really appreciate a more point specific answer.
So, its more of a matter if you know any way I could remove everything but the 'src="' value of the K2 Extra Fields Image in the item.php?
And I'm not really a PHP developer. I try to work my way around many times by testing millions of possibilities until it works, but I would really appreciate a more point specific answer.
So, its more of a matter if you know any way I could remove everything but the 'src="' value of the K2 Extra Fields Image in the item.php?
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
- Offline
- Platinum Member
Less
More
- Posts: 15920
9 years 1 month ago #148018
by Krikor Boghossian
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Replied by Krikor Boghossian on topic K2 Extra Fields Image Tag Src Extract
I already sent you the code. Look at the highlighted section.
Replace the item.php's image block with and change the alias to your extrafield's alias.
Replace the item.php's image block with and change the alias to your extrafield's alias.
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- Design
- Topic Author
- Offline
- New Member
Less
More
- Posts: 13
9 years 1 month ago #148022
by Design
Replied by Design on topic K2 Extra Fields Image Tag Src Extract
So you're saying that if I use this:
The value of the image will come out as a pure string of the source, and not an IMG tag?
If so, I'll test it out later.
<?php echo $this->item->extraFields->EXTRAFIELDALIASHERE->value; ?>
The value of the image will come out as a pure string of the source, and not an IMG tag?
If so, I'll test it out later.
Please Log in or Create an account to join the conversation.
- Design
- Topic Author
- Offline
- New Member
Less
More
- Posts: 13
9 years 1 month ago #148035
by Design
Replied by Design on topic K2 Extra Fields Image Tag Src Extract
Hey! I tried combining many of these techniques, and it actually was working, but I stumbled on something really weird.
I started the item with this:
I declared $icon and use $extrafieldsalias[28] to get the alias of the the Extra Field I wanted.
When I used preg_replace on the '/<img src="/', it worked and the actual <img src=" was removed from the $icon string as I desired, but the second part: '/" alt="Icon" />/' did not work as I expected. When I add this part, it actually erases the entire content and nothing shows up.
Do you have any idea why that is? I'm almost certain that it has nothing to do with the PHP function being wrongly written as it has been working and I got a few results. Well... Anything I could work around or any testing?
I started the item with this:
$icon = $this->item->extraFields->$extrafieldsalias[28]->value ;
$icon = preg_replace(array('/<img src="/', '/" alt="Icon" />/'),"",$icon);
I declared $icon and use $extrafieldsalias[28] to get the alias of the the Extra Field I wanted.
When I used preg_replace on the '/<img src="/', it worked and the actual <img src=" was removed from the $icon string as I desired, but the second part: '/" alt="Icon" />/' did not work as I expected. When I add this part, it actually erases the entire content and nothing shows up.
Do you have any idea why that is? I'm almost certain that it has nothing to do with the PHP function being wrongly written as it has been working and I got a few results. Well... Anything I could work around or any testing?
Please Log in or Create an account to join the conversation.
- Design
- Topic Author
- Offline
- New Member
Less
More
- Posts: 13
9 years 1 month ago #148037
by Design
Replied by Design on topic K2 Extra Fields Image Tag Src Extract
Nevermind! Finally managed to do it!
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
- Offline
- Platinum Member
Less
More
- Posts: 15920
9 years 1 month ago #148156
by Krikor Boghossian
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Replied by Krikor Boghossian on topic K2 Extra Fields Image Tag Src Extract
preg_replace requires a wildcard character to work correctly in this case.
or getk2.org/documentation/tips-a-tricks/1129-extra-field-image-type-as-link which you might have already seen.
$toreplace = array(
'<img src="(.*?)" alt="Icon" />') =>'$1'
);
or getk2.org/documentation/tips-a-tricks/1129-extra-field-image-type-as-link which you might have already seen.
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- Design
- Topic Author
- Offline
- New Member
Less
More
- Posts: 13
9 years 1 month ago #148158
by Design
Replied by Design on topic K2 Extra Fields Image Tag Src Extract
Thanks for the help Krikor, but as I said before, I managed to solve it combining some functions I found around!
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
- Offline
- Platinum Member
Less
More
- Posts: 15920
9 years 1 month ago #148160
by Krikor Boghossian
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Replied by Krikor Boghossian on topic K2 Extra Fields Image Tag Src Extract
Can you post your solution for reference?
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.