Keyword

SOLVED: Tutorial for extra fields?

More
11 years 7 months ago #103215 by Napolian Stewart
Replied by Napolian Stewart on topic Re: Tutorial for extra fields?
Scharfet,

Try it without the single quotes around the IDs, like this - [1] - instead of this - .

This works for me, except the tabs don't render for me.
Log in  or Create an account to join the conversation.

  • Johann Scharfetter
  • Johann Scharfetter's Avatar Topic Author
  • Offline
  • Senior Member
More
11 years 7 months ago #103216 by Johann Scharfetter
Replied by Johann Scharfetter on topic Re: SOLVED: Tutorial for extra fields?
Hi Poe,

it is working now. You were right the code had errors.

For others who wants to know how to use "Extra Fields" in a personalized way in "Item view".
In "Category" > "Item View Options " > "Extra Fields" > it is necesary to choose "Show".
The extra fields will be displayed also in the standard way (in a table). This code need to be deleted forom item.php.

At the beginning of the item.php place following code.
Log in  or Create an account to join the conversation.

More
11 years 5 months ago #103217 by Odin Mayland
Replied by Odin Mayland on topic Re: SOLVED: Tutorial for extra fields?
I can get this to work in category.php but only at the end of the code. I want the extrafields to appear after the subcategories and before the items.

The first screen shot shows the code working after the item list:

Attachment not found





The second screenshot shows the same code in the same category.php file but the extrafields are not showing:

Attachment not found




I can't figure out why the same code will not work in a different location.
Attachments:

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

More
10 years 1 month ago - 10 years 1 month ago #103218 by Maciej Charabin
Replied by Maciej Charabin on topic Re: SOLVED: Tutorial for extra fields?
Hi there,

I'm using this part of php code in my template. So I have title and subtitle (extra fields) in my k2 items.

I'd like to use it in this way:

Every item has a title and I want to display it on home page. So when user click on the main page and open k2 item, I'd like that he would see a subtitle from extra fields instead of item title. But when item has no subtitle (extra fields), he would see item title....

Maybe someone help..

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

More
10 years 1 month ago #103219 by JoomlaWorks
Replied by JoomlaWorks on topic Re: SOLVED: Tutorial for extra fields?
You will need to modify the item.php subtemplate in K2.

First read this on K2 templating in general: getk2.org/documentation/tutorials/174-templating-with-k2-and-the-concepts-of-sub-templates

Then edit your item.php file and locate the part where the title is shown. We'll add a small check so we can display the subtitle instead.

Create your subtitle extra field, add it to a group and make sure this group is also assigned to one or more categories in K2. Let's say this extra field is simply called "Subtitle" in which case, it will get the alias "Subtitle" as well. We will use this "alias" to directly fetch and display the subtitle value if it exists.

Find this (~line 54):
<?php echo $this->item->title; ?>

and replace with this:
<?php if($this->item->extraFields->Subtitle->value!=''): ?>
<?php echo $this->item->extraFields->Subtitle->value; ?>
<?php else: ?>
<?php echo $this->item->title; ?>
<?php endif; ?>

This will check if the Subtitle extra field is set. If it is, it will display it, otherwise it will display the K2 item title.

Simple.

For more on this neat trick read the 2.6.2 release post: getk2.org/blog/1068-k2-v262-now-available

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

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

More
10 years 1 month ago #103220 by Maciej Charabin
Replied by Maciej Charabin on topic Re: SOLVED: Tutorial for extra fields?
YEAH!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! :)

You're the best! :)

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

More
10 years 1 month ago #103221 by Maciej Charabin
Replied by Maciej Charabin on topic Re: SOLVED: Tutorial for extra fields?
Hi :)

Only one question...

Code works perfectly, but now when we have subtitle (extrafields), so it displays on the item title position (that's OK) and also on the extra fields.

Maybe someone know, how to display this subtitle (extrafield) on item title and at the same time not display in the extra fields position...

This is extra field code from item php:
<?php if($this->item->params->get('itemExtraFields') && count($this->item->extra_fields)): ?>
	  <!-- Item extra fields -->
	  <div class="itemExtraFields">
	  	<h3><?php echo JText::_('K2_ADDITIONAL_INFO'); ?></h3>
	  	<ul>
			<?php foreach ($this->item->extra_fields as $key=>$extraField): ?>
			<?php if($extraField->value != ''): ?>
			<li class="<?php echo ($key%2) ? "odd" : "even"; ?> type<?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">
				<?php if($extraField->type == 'header'): ?>
				<h4 class="itemExtraFieldsHeader">
				<?php echo $extraField->name; ?></h4>
				<?php else: ?>

				<span class="itemExtraFieldsLabel"><?php echo $extraField->name; ?>:</span>
				<span class="itemExtraFieldsValue"><?php echo $extraField->value; ?></span>
				<?php endif; ?>
			</li>
			<?php endif; ?>
			<?php endforeach; ?>
			</ul>
	    <div class="clr"></div>
	  </div>
	  <?php endif; ?>

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

More
10 years 1 month ago #103222 by JoomlaWorks
Replied by JoomlaWorks on topic Re: SOLVED: Tutorial for extra fields?
Under the foreach loop in PHP, do this (I'm only editing what's necessary - adapt as you wish):
<?php foreach ($this->item->extra_fields as $key=>$extraField): ?>

<?php if($extraField->name=='Subtitle') continue; ?>
...
<?php endforeach; ?>

This will cause the foreach loop to skip when it comes to a "Subtitle" extra field. ;)

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

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

More
10 years 1 month ago #103223 by Maciej Charabin
Replied by Maciej Charabin on topic Re: SOLVED: Tutorial for extra fields?
I've made it like this:
<?php if($this->item->params->get('itemExtraFields') && count($this->item->extra_fields)): ?>
	  <!-- Item extra fields -->
	  <div class="itemExtraFields">
	  	<h3><?php echo JText::_('K2_ADDITIONAL_INFO'); ?></h3>
	  	<ul>
			

<?php foreach ($this->item->extra_fields as $key=>$extraField): ?>
<?php foreach ($this->item->extra_fields as $key=>$extraField): ?>
<?php if($extraField->name=='Podtytul') continue; ?>
<?php endforeach; ?>




			
			<?php if($extraField->value != ''): ?>


			<li class="<?php echo ($key%2) ? "odd" : "even"; ?> type<?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">
				<?php if($extraField->type == 'header'): ?>
				<h4 class="itemExtraFieldsHeader"><?php echo $extraField->name; ?></h4>
				<?php else: ?>
				
				<span class="itemExtraFieldsLabel"><?php echo $extraField->name; ?>:</span>
				<span class="itemExtraFieldsValue"><?php echo $extraField->value; ?></span>
				<?php endif; ?>
			</li>
			<?php endif; ?>

			<?php endforeach; ?>
			</ul>

But extra field wchich namie is "Podtytul" still exist in extra fields position... :( Please help...

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

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
10 years 1 month ago #103224 by Krikor Boghossian
Replied by Krikor Boghossian on topic Re: SOLVED: Tutorial for extra fields?
You do need a second
<?php foreach ($this->item->extra_fields as $key=>$extraField): ?>

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

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