Wednesday, May 23rd

Last update05:36:30 AM GMT

Font Size

Screen

Profile

Layout

Menu Style

Cpanel
Welcome, Guest
Username Password: Remember me

K2 Custom Fields
(1 viewing) (1) Guest
  • Page:
  • 1

TOPIC: K2 Custom Fields

K2 Custom Fields 11 months, 3 weeks ago #4339

Hi everyone,

We're still having issues calling our K2 extra fields into our templates. I'm still using the code I posted a week or so ago, and it works on occasion but most of the time we get nothing:

We're attempting to put together a site that will allow the client to simply add their content to a series of extra fields, which will be preformatted by us with a number of different templates. One of them for example, is a 'Celebrity of the Month' item. We want to create a series of fields (Name, Age, Nationality, Eye Colour, etc), and then position the values within the main section of the article.

This is the code we were instructed to use to call our extra fields into our template.

<?php echo $this->item->extra_fields[0]->value; ?>


The instructions we were given simply said to adjust the field number to correspond with the required fields position in the back end editor. This works for our first extra field [0], but we can't get any of our other fields to display at all.

If anyone has an idiot proof method for achieving what we're after, I'd be super keen on hearing about it. Thanks, in advance, because at the moment I'm officially stumped.
Last Edit: 11 months, 3 weeks ago by chrishiggins.

Re: K2 Custom Fields 11 months, 1 week ago #4396

  • Partic
  • OFFLINE
  • Moderator
  • Posts: 341
  • Karma: 21
Your snippet is correct - have about 20 instances of that with different values in my category template.

The value is the position in the array, not the ID of the Custom Field in K2... which is what you might be perhaps doing. The array is populated with only the custom fields that are assigned to the custom profile group you're displaying.

The ordering in the array is based on the ordering of the custom fields when you look at the list of Custom Fields in K2. Reordering the fields will rearrange the values in the array. So basically the number to use is the position of the field in your list minus 1 to take into account that the array starts at 0.

There's probably a better way to reference the custom fields to ensure that ordering changes don't break your template, but at the moment, I've not looked into it further having gotten the above to work.
Patrick Jackson
www.kpsystems.com.au
Ask Anything Joomla and I'll helpfully tell you where to go

Re: K2 Custom Fields 4 months, 2 weeks ago #5496

  • MartinB
  • OFFLINE
  • Expert Boarder
  • Posts: 90
  • Karma: 0
Sorry to reopen such an old post but this is just what I'm looking at the moment.

I'm using extra fields and what to be able to pick out a specific field to display in a specific location in the item page.

I have a set number of extra fields however not ever item page will use all of the avaliable extra fields, which I guess means not noing the extra field ID to reference - is that right ?

Would it be possible to check the content's of the field in someway and then when it matches the condition display it ?
Is there another easier way of targeting a speciifc extra field ?

Re: K2 Custom Fields 4 months, 2 weeks ago #5497

  • MartinB
  • OFFLINE
  • Expert Boarder
  • Posts: 90
  • Karma: 0
Ok, was a little easier in the end than I thought, however like all these things it takes hours and hours to find a quick answer

Rather than explain the solution I'll refer you to the link that helped me -

getk2.org/documentation/tips-a-tricks/it...fic-extra-field-data

More Joomle/K2 info here - jurawa.com/notes

Re: K2 Custom Fields 4 months, 2 weeks ago #5498

  • Partic
  • OFFLINE
  • Moderator
  • Posts: 341
  • Karma: 21
5hrs to find it yourself isn't bad Martin... but seeing 72 hrs before I was doign the exact same thing you were doing, you should have skyped

I hope Norm Douglas doens't mind me sharing his snazzy bit of code that he shared with me after the MJUG K2 session a few months ago.

Basically, to start off, you put this code at the top of your K2 item.php file (or whichever file you're wanting to show the K2 extra fields in - other options include category_item.php, and with a bit of tweaking, you can also put the extra fields into the main joomla search results, but that requires copying a few K2 functions at this stage).

This code cycles through the array of key fields, and creates a new array called $field. The values of the array are then the field name, and the field value.
 
<?php foreach ($this->item->extra_fields as $key=>$extraField):
$field = $extraField->name;
${"field_{$field}"} = $extraField->value;
endforeach; ?>
 


In the layout you then use the extra field's name from K2, rather than needing to know the item ID of the field. It then will display that field name's value from the array created above.
<?php echo ${'field_Location'}; ?>


Or to then display fields with a special format you could do something like this for a price field to add a $ in front:
<?php echo '$'.number_format(${'field_Price'}); ?>


To tie in another question asked I think by Craig and Ant on another section here, you can then look at the value of the extra field before deciding to display it - that way you avoid floating labels with no values:
 
<?php if (${field_Location) : ?>
<div class="extrafieldlabel">Location:</div><div class="extrafieldvalue"><?php echo ${'field_Location'}; ?></div>
<?php else: ?>
<div class="extrafieldlabel">No location specified for this entry.</div>
<?php endif;?>
 
Patrick Jackson
www.kpsystems.com.au
Ask Anything Joomla and I'll helpfully tell you where to go

Re: K2 Custom Fields 1 month ago #6097

I have successfully used the following code in the item.php

I have tried your code but I am getting an error on the frontend for the category_item.php.

Here is my code:
 
<!-- K2 Plugins: K2BeforeDisplayContent -->
 
<!-- Call to prepare extra fields -->
<?php foreach ($this->item->extra_fields as $key=>$extraField):
$field = $extraField->name;
${"field_{$field}"} = $extraField->value;
endforeach; ?>
 
<?php echo ${'field_National Stock Number'}; ?>


The extrafield name is "National Stock Number" and is ID #1.

This is the error on the frontend:
"Warning: Invalid argument supplied for foreach() in .........../templates/rt_ionosphere/html/com_k2/templates/fastpacks/category_item.php on line 108"

Line 108 is
<?php foreach ($this->item->extra_fields as $key=>$extraField):


Can you please help me?
Last Edit: 1 month ago by modernmagic.

Re: K2 Custom Fields 1 month ago #6098

Its always something simple..... like not setting Extrafields to "Show".

Sorry.

Re: K2 Custom Fields 3 weeks, 5 days ago #6110

  • Spanka
  • OFFLINE
  • Fresh Boarder
  • Posts: 1
  • Karma: 0
Hi guys - my first post here I've been wrestling with k2 for a day or so now. Good times. I googled allowing a default value in an extra field to contain HTML (wanted to output a table structure for completion...). I came across this topic instead and thought I might share what I've done as an alternate method to get around the items discussed in this post.

fair warning: I'm no coder, barely conversant with php in the scheme of things and this code may well be the root of all evil. I can't seem to find the preview button, so I hope I can post-post edit

General idea: k2 presumes you want to loop out all your extra fields in one fell swoop. Largely, this is the case for me, but there's a few fields I want to do other stuff with (say, a property ID!). In that case, I need to exclude certain extra fields from the main loop AND get values from those excluded fields for use elsewhere *without* looping.

Step 1: set up an exclusion list and set up some vars up front that I'll use later.
 
/*
prepare specific blocks.
Instead of traversing array every time for ONE custom field, loop once and setup code output.
custom field exceptions - those fields we don't want output in the usual fashion */

$fieldexceptions = "|Property Tagline|Property ID|Main Image|Main Image Caption";
 
$enquire = "<a href='http://www.mydomainname.com.au/enquiries.php?apart=".$this->item->title."' title='Enquire about ".$this->item->title."'>Enquire Now</a>";
$shortlist = "";
$booknow = "";
$tagline = "";
$mainimage = "";
$mainimagecaption = "";
 
if($this->item->params->get('itemExtraFields') && count($this->item->extra_fields)) {
foreach ($this->item->extra_fields as $key=>$extraField){
if($extraField->value) {
if ($extraField->name == "Property ID") {
$shortlist = "<a href=\"javascript:addpropertytoshortlist(".$extraField->value.",'".$this->item->title."','".$this->item->link."')\">Shortlist It!</a>";
$booknow = "<a href='https://www.mydomainname.com.au/book.php?apart=".$extraField->value."' title='Book ".$this->item->title." for your Hamilton Island accommodation!'>Book Now</a>";
} elseif($extraField->name == "Property Tagline") {
$tagline = "<h3 class='tagline'>".$extraField->value."</h3>";
} elseif($extraField->name == "Main Image") {
$mainimage = $extraField->value;
} elseif($extraField->name == "Main Image Caption") {
$mainimagecaption = $extraField->value;
}
}
}
}
 
 


Step 2: Modify the core extra fields loop in my sub-template to skip those fields I want skipped as I'll use them elsewhere

 
<?php if($extraField->value && strpos($fieldexceptions, $extraField->name) < 1 ): ?>
 


Step 3: Do normal php stuff...
 
<div class="myTools">
<?php echo $enquire; ?>
</div>
<div class="myTools">
<?php echo $shortlist; ?>
</div>
<div class="myTools">
<?php echo $booknow; ?>
</div>
 


Net result: most of my extra fields loop out in the normal scheme of things, but I have simple php vars that I can use anywhere I please, without having to mess around indexing arrays or anything else that is probably over my head

Anyway - I hope this helps someone out there - its rare I get to give back, but it seems like my most recent efforts may acutally help someone here!
Last Edit: 3 weeks, 5 days ago by Spanka.
  • Page:
  • 1
Time to create page: 1.71 seconds
You are here: Forum