Keyword

[SOLVED] Replace itemtitle with an extra field if it exists

  • neppers
  • neppers's Avatar Topic Author
  • Offline
  • Junior Member
More
10 years 6 months ago #126039 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; ?>

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

More
10 years 6 months ago - 10 years 6 months ago #126040 by Lefteris
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 :
<?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
  • neppers's Avatar Topic Author
  • Offline
  • Junior Member
More
10 years 6 months ago #126041 by neppers
That did it :-) Thanks.

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

More
10 years 6 months ago #126042 by Lefteris
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.


Powered by Kunena Forum