Keyword

How to display extra fields in category.php

  • Uldis
  • Uldis's Avatar Topic Author
  • Offline
  • Senior Member
More
11 years 7 months ago - 11 years 7 months ago #111621 by Uldis
Hello,

I noticed that there are some big changes for extra fields and that you can now output an extra field using -
$this->item->extraFields->EXTRAFIELDALIASHERE->value
, which is great.

But I would like to display extra field values in category.php.

For example:
I'm in category with 3 items.
Each item has an extra field with value a, value b, value c
On top of category (category.php) I would like to output - value a, value b, value c.

I'm trying to create a simple sorting solution.

I tried using this code - I found it in forum
<!-- Call to prepare extra field IDs --> 
          <?php 
     //convertArray to use ids as key
          $extrafieldsid = array();
          foreach($this->item->extra_fields as $itemid)
          {    
          $extrafieldsid[$itemid->id] = $itemid->value;
          }   
          ?>
<!-- Call to prepare extra field labels -->
           <?php 
     //convertArray to use labels as key
          $extrafieldslabels = array();
          foreach($this->item->extra_fields as $itemlabel)
          {    
          $extrafieldslabels[$itemlabel->id] = $itemlabel->name;
          }   
          ?>

and
<!-- Extra Fields -->
<div class="extrafield">
          <strong><?php echo $extrafieldslabels[1];?>: </strong><?php echo $extrafieldsid[1];?>
</div>
<div class="extrafield">
          <strong><?php echo $extrafieldslabels[2];?>: </strong><?php echo $extrafieldsid[2];?>
</div>
<div class="clr"></div>
 
<!-- End Extra Fields -->

But it seems that either I did something wrong or this code doesn't work with newer k2 versions.

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

More
11 years 7 months ago #111622 by Peter Grube
Replied by Peter Grube on topic Re: How to display extra fields in category.php
Hi

I used
Log in  or Create an account to join the conversation.

More
11 years 7 months ago - 11 years 7 months ago #111623 by designinside
Replied by designinside on topic Re: How to display extra fields in category.php
I used this currently in the item.php & category_item.php:

In the head section before the layout starts:
Log in  or Create an account to join the conversation.

  • Uldis
  • Uldis's Avatar Topic Author
  • Offline
  • Senior Member
More
11 years 7 months ago #111624 by Uldis
Thank you both for replying.

Chris, your solution works great on category_item.php or item.php.

But I'm looking for a little bit different solution.

I will try to clarify.

Lets says I have category structure like this:
Fruit
|_Apples (with extra field color)
|_Oranges (with extra field color)

When I enter category Fruit I can display extra fields for each item within item (category_item.php), this is a k2 function.

But I would like to output a list of extra fields outside of item loop (category_item.php). I would like to output extra field values in category.php.

So in my example, at the top of category view I would have unordered list with - green (from apples extra field) and orange (from oranges extra field)
But I would like to output a list

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

More
11 years 7 months ago #111625 by Web Guy
Hi All, doing some hacking in a template over-ride of category.php, I am not a PHP programmer, so, need to keep that in mind please.

I have altered my category.php to display the list of articles in a table with the columns.

The first column goes the article name(works fine)
2nd column I want the location custom field
3rd column another value

Here is what I have tried, it is obviously wrong as all I get is repeats of the article name...

<?php
// Load category_item.php by default
$this->item->extraFields->Location->value;
echo $this->loadTemplate('item');
?>
What do I need to change, so that it actually outputs the value of the extra filed with the alias of "Location"?

Cheers

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

More
11 years 7 months ago #111626 by Web Guy
Anyone have any ideas on this? Kinda stuck til I can sort this out.

Cheers

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

More
11 years 7 months ago #111627 by DREAMBUILDS
Replied by DREAMBUILDS on topic Re: How to display extra fields in category.php
I can not thank you enough. This code saved my life B)

Chris wrote: I used this currently in the item.php & category_item.php:

In the head section before the layout starts:

Log in  or Create an account to join the conversation.

More
11 years 3 months ago #111628 by klikeri
I inseet code in category_item.php but i get error
Warning: Invalid argument supplied for foreach() in category_item.php on line 17

pls help

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