- Posts: 28
COMMUNITY FORUM
- Forum
- K2 Community Forum
- English K2 Community
- [SOLVED] Replace itemtitle with an extra field if it exists
[SOLVED] Replace itemtitle with an extra field if it exists
- neppers
- Topic Author
- Offline
- Junior Member
Less
More
10 years 6 months ago #126039
by neppers
[SOLVED] Replace itemtitle with an extra field if it exists was created by neppers
I am trying go use an alternative item title on the categories-page. I have inserted this code at the top so I can insert extra-fields where I want:
<?php
$extrafields = array();
foreach($this->item->extra_fields as $item)
{ $extrafields[$item->id] = $item->value; }
?>
Then insert the field with <?php echo $extrafields[1]; ?>
The [1] is the numeric ID of the field. So far so good. Now I am trying to create the code so that if this field is not empty it will show the extrafield as the item title. If it is empty I want it to show the normal title. I have started with this code, but I guess I am doing something wrong. It always show the normal title, regardless of the extrafield is empty or not.
<?php if(empty($this->item->extrafields[3])): ?>
<?php echo $this->item->title; ?>
<?php else: ?>
<?php echo $extrafields[3];?>
<?php endif; ?>
<?php
$extrafields = array();
foreach($this->item->extra_fields as $item)
{ $extrafields[$item->id] = $item->value; }
?>
Then insert the field with <?php echo $extrafields[1]; ?>
The [1] is the numeric ID of the field. So far so good. Now I am trying to create the code so that if this field is not empty it will show the extrafield as the item title. If it is empty I want it to show the normal title. I have started with this code, but I guess I am doing something wrong. It always show the normal title, regardless of the extrafield is empty or not.
<?php if(empty($this->item->extrafields[3])): ?>
<?php echo $this->item->title; ?>
<?php else: ?>
<?php echo $extrafields[3];?>
<?php endif; ?>
Please Log in or Create an account to join the conversation.
- Lefteris
- Offline
- Moderator
Less
More
- Posts: 8743
10 years 6 months ago - 10 years 6 months ago #126040
by Lefteris
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Replied by Lefteris on topic Re: Replace itemtitle with an extra field if it exists
Hi. The code seems correct. However i suggest that you use the extra field alias which was introduced in the latest K2 versions. Then your code would be :
This assumes that your extra field used for title has the alias "extraFieldTitle".
<?php if(trim($this->item->extraFields->extraFieldTitle->value) == '' ): ?>
<?php echo $this->item->title; ?>
<?php else: ?>
<?php echo $this->item->extraFields->extraFieldTitle->value; ?>
<?php endif; ?>
This assumes that your extra field used for title has the alias "extraFieldTitle".
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- neppers
- Topic Author
- Offline
- Junior Member
Less
More
- Posts: 28
10 years 6 months ago #126041
by neppers
Replied by neppers on topic Re: Replace itemtitle with an extra field if it exists
That did it :-) Thanks.
Please Log in or Create an account to join the conversation.
- Lefteris
- Offline
- Moderator
Less
More
- Posts: 8743
10 years 6 months ago #126042
by Lefteris
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Replied by Lefteris on topic Re: Replace itemtitle with an extra field if it exists
You are welcome.
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- Forum
- K2 Community Forum
- English K2 Community
- [SOLVED] Replace itemtitle with an extra field if it exists