Keyword

Can't add a hyperlink around an extra field

More
8 years 5 months ago #148883 by JP
Hello,
I am have custom templates setup for both the search results (using K2 Tools) and the K2 Content modules. I am using the following array to display extra fields in a custom layout on both the search page and within the K2 Content module (which is showing a featured item only):
<?php
//convertArray to use ids as key
$extrafields = array();
foreach($item->extra_fields as $item)
{
$extrafields[$item->id] = $item->value;
}
?>

What I want to do is add the hyperlink to the displayed item on the First Name extra field. I have the following code for generic.php and default.php to achieve this:

<li><a href="<?php echo $item->link; ?>">
<h5 class="name"><?php echo $extrafields[1];?></h5></a>
</li>

The link is redirecting to the home page, and not to the specific item. I have tried using <a href="<?php echo $this->item->link; ?>"> and adding "this" to the array, but the page breaks.
Can you please assist?

Thank you!

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

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
8 years 5 months ago #148909 by Krikor Boghossian
Replied by Krikor Boghossian on topic Can't add a hyperlink around an extra field
Since you are using $item inside the scope of your own function, the $item->link will try to fetch the link property of the extrafield's object and not the (correct) link of the item's object.

I would rephrase
foreach($item->extra_fields as $item)

to:
foreach($item->extra_fields as $extra_field)
and use $extra_field instead of $item.

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

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

More
8 years 5 months ago #148913 by JP
Thanks for the assistance, but that did not resolve the issue. I've changed the code to the following, as instructed:

<!-- Call to prepare extra fields -->
<?php
//convertArray to use ids as key
$extrafields = array();
foreach($item->extra_fields as $extra_field)
{
$extrafields[$extra_field->id] = $extra_field->value;
}
?>

and:

<li><a href="<?php echo $extra_field->link; ?>">
<h5 class="name"><?php echo $extrafields[1];?></h5></a>
</li>

Still having the same issue. The link goes to the home page and not the item URL. Is this the correct code? If not, do you have a different solution?

Thanks very much!

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

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
8 years 5 months ago - 8 years 2 months ago #148925 by Krikor Boghossian
Replied by Krikor Boghossian on topic Can't add a hyperlink around an extra field
<li><a href="<?php echo $item->link; ?>">
<h5 class="name"><?php echo $extrafields[1];?></a></h5>
</li>

This will point to the item's link.

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Last edit: 8 years 2 months ago by Krikor Boghossian. Reason: typo in the code

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

More
8 years 5 months ago #148928 by JP
Fantastic! Works just as I'd like it to now. Thank you for your help!

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

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
8 years 5 months ago #148945 by Krikor Boghossian
Replied by Krikor Boghossian on topic Can't add a hyperlink around an extra field
You're welcome :)

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

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

More
8 years 2 months ago #151328 by Dave W
Replied by Dave W on topic Can't add a hyperlink around an extra field
I have a similar need. Looking to add a Google Analytics click event to track outgoing link clicks. The extra field is a social media destination.

I have used your code below to start and having some difficulty with the proper code. I'll keep working on it but thought the community can offer some guidance.

Sample code format from Google:
<a href="www.example.com" onclick="trackOutboundLink('www.example.com'); return false;">Check out example.com

Using Extra Field format - work in progress:
<a href="<?php echo $extrafields[18];?>" onclick="trackOutboundLink('<?php echo $extrafields[18];?>'); return false;"><?php echo $extrafields[1];?></a>

- Extra field [18] = Social Media website
- Extra field [1] = Name of the K2 item or in a directory, one of the directory names. i.e. Mary's Coffee Shop

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

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
8 years 2 months ago #151339 by Krikor Boghossian
Replied by Krikor Boghossian on topic Can't add a hyperlink around an extra field
Hello,

Can you c/p the entire code?

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

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

More
8 years 2 months ago #151351 by Dave W
Replied by Dave W on topic Can't add a hyperlink around an extra field
Have seen 95% success and have the click event tracking in Google Analytics. Here is code:

<?php if($extrafields[22]!=''):?> <!-- if field is not empty, then call data -->
<li><span class="extralabel">Social Media Links:</span><span class="extravalue"><a href="<?php echo $extrafields[22];?>" onclick="trackOutboundLink('<?php echo $extrafields[22];?>'); return false;" target="_blank">Facebook</a></span></li>
<?php endif; ?>

Notes:
- Opening in new window. target="_blank" <- not working
- Extra field [22] = Extra Field type=Text. Outgoing Facebook link
- The script is separate and added at top of item.php file.
<script type="text/javascript">
var trackOutboundLink = function(url) {
ga('send', 'event', 'outbound', 'click', url, {
'transport': 'beacon',
'hitCallback': function(){document.location = url;}
});
}
</script>

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

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
8 years 2 months ago #151364 by Krikor Boghossian
Replied by Krikor Boghossian on topic Can't add a hyperlink around an extra field
I would urge you to restructure your code to something like this -> github.com/kricore/Advanced-templating-with-K2/blob/master/_inc/cheatsheet.php#L105-L109

Depending on the context (item, category item etc) you may need to use $this->item or $item

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

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


Powered by Kunena Forum