Keyword

Top Tip for templating K2's extrafields

  • Alan Sparkes
  • Alan Sparkes's Avatar Topic Author
  • Offline
  • Premium Member
More
13 years 7 months ago #93959 by Alan Sparkes
Top Tip for templating K2's extrafields was created by Alan Sparkes
I love K2. I really love it. Often when we have a complex design and want to use extrafields we come across a recurring problem.

We want to use extrafields for acheiving some different layouts but we dont want them all at once.

Currently you have to loop thru the fields and if you want only 3 fields at the top of your template and a different 2 fields somewhere else in your layout its tempting to just loop thru and check if the fieldyou want is there then do your output.

Eg 

foreach ($this->item->extra_fields as $key=>$extraField):                      if $myfield ='what i want' echo $myfieldvalue;

endforeach;                                                ?>

 

Well I didnt want to do this any more.

So here is an alternative way of calling the extrafields.

 

Put this at the top of your K2 item template

//setup new array

$exFields = array();

//loop thru extrafields and build collection of key and values

foreach ($this->item->extra_fields as $key=>$extraField):     $exFields[$extraField->name] = $extraField->value;

   endforeach; 

 

Now you can access each extrafield directly by its name like this without looping

<?php echo $exFields["extraFieldName"]; ?>

 

So if your extrafield was called 'Address" then you would use

<?php echo $exFields["Address"]; ?>

to output the value.

 

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

More
13 years 7 months ago #93960 by Jeff Whitmore
Replied by Jeff Whitmore on topic Top Tip for templating K2's extrafields
Sweet!

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

More
13 years 7 months ago #93961 by william white
Replied by william white on topic Top Tip for templating K2's extrafields
Make a blog post about this!

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

More
13 years 6 months ago #93962 by william white
Replied by william white on topic Top Tip for templating K2's extrafields
try

this->extrafield->name
Frank said:

Great!! Thanks a lot for this tip. I'm not really a great php programmer, so I need to ask this: can you provide the code for displaying the name? The value output works fine, but I don't know how to extract the name.

 

Thanks in advance! FRANK CZYGAN

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

More
13 years 6 months ago #93963 by xtreme07
Replied by xtreme07 on topic Top Tip for templating K2's extrafields
how do i do this for field types ? .. so i can have extraFieldType instead of extraFieldName

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


Powered by Kunena Forum