- Posts: 69
COMMUNITY FORUM
Display specific extra field in module position ?
- Uldis
-
Topic Author
- Offline
- Senior Member
I've been looking around forum for a solution, but it seems that this question hasn't been answered yet.
I'm looking for ways to display content of specific extra field in a module position.
I found this in k2 tips - link
So I tried using this code
Log in or Create an account to join the conversation.
- william white
-
- Offline
- Platinum Member
- Posts: 3722
does something similar to what you want very nicely.
You may be able to override it to show only what you want, or get the code from it to link a module with the current item
Please Log in or Create an account to join the conversation.
- Uldis
-
Topic Author
- Offline
- Senior Member
- Posts: 69
I was just editing: my first post.
I'm actualy already using k2 split content to display 1 extra field in module position, but I need to display another extra field in diferent module position, k2 split content can only display all extra fields or none.
I tried using code mentioned above in k2 split content override as well, but that didn't work out for me.
Please Log in or Create an account to join the conversation.
- william white
-
- Offline
- Platinum Member
- Posts: 3722
You should be able to create a subtemplate for it, and change the foreach loop that displays extra fields to do what you want, and select the subtemplate in the module instance
Please Log in or Create an account to join the conversation.
- Uldis
-
Topic Author
- Offline
- Senior Member
- Posts: 69
Log in or Create an account to join the conversation.
- william white
-
- Offline
- Platinum Member
- Posts: 3722
<?php foreach ($item->extra_fields as $extraField): ?>
<li class="type<?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">
<span class="moduleItemExtraFieldsLabel"><b><?php echo $extraField->name; ?></b></span>
<span class="moduleItemExtraFieldsValue"><?php echo $extraField->value; ?></span>
<br class="clr" />
</li>
<?php endforeach; ?>
and insert a condition to only print out the extra field you want
Please Log in or Create an account to join the conversation.
- william white
-
- Offline
- Platinum Member
- Posts: 3722
<?php foreach ($item->extra_fields as $extraField): ?>
<?php if($extraField->name == "ExtraFieldName"): ?>
<li class="type<?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">
<span class="moduleItemExtraFieldsLabel"><b><?php echo $extraField->name; ?></b></span>
<span class="moduleItemExtraFieldsValue"><?php echo $extraField->value; ?></span>
<br class="clr" />
</li>
<?php endif;?>
<?php endforeach; ?>
Please Log in or Create an account to join the conversation.
- Uldis
-
Topic Author
- Offline
- Senior Member
- Posts: 69
thank you very much this is exactly what I wanted to achieve.
I don't know php yet, but I want to learn it very much.
Thank you for providing code I needed !
Please Log in or Create an account to join the conversation.
- Steven Johnson
-
- Offline
- Senior Member
- Posts: 69
What does the php code look like to insert a condition to print out a specific field?
Thanks! -- Steven
I am on Joomla 2.5.7 and K2 2.5.7
EDIT
After reading this post, I do not think this is 100% my situation. I am trying to display the k2 content module on the home page. In this module I want to display a single custom field for each of the different items
When I and the code to prepare extra field
Log in or Create an account to join the conversation.
- william white
-
- Offline
- Platinum Member
- Posts: 3722
Please Log in or Create an account to join the conversation.
- danny leary
-
- Offline
- New Member
- Posts: 5
I think i have the solution for using custom extrafields from k2 inside the k2 content module.
The instructions given here do work
jbeginner.com/tutorials/extensions/how-to-display-k2-extra-fields/
but with a slight adjustment to the code to start the call. this following code should work.
<?php
//convertArray to use ids as key
$extrafields = array();
foreach($item->extra_fields as $item)
{
$extrafields[$item->id] = $item->value;
}
?>
Basically, just removed the $this-> in the code given at the above url. the code given at the above url works great for the content items and templates, but for some reason breaks the site with a white screen in sub-templates created for the k2 content module.
This code should work. tested in a joomla 2.5.7 installation. the echo code given in the above example should work as well after the modified call code to actually show extra field data via id number.
Best Regards,
Danny
shoutoutadvertising.com
Please Log in or Create an account to join the conversation.
- Lefteris
-
- Offline
- Moderator
- Posts: 8743
Since K2 2.6.2 you can render an extra field using it's alias. Read the release announcement for more information getk2.org/blog/item/1068-k2-v262-now-available
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- danny leary
-
- Offline
- New Member
- Posts: 5
one question that i couldnt' ascertain from the article.... will this method work for module sub-templates too?
those using older versions of k2 with lots of hacks might still be able to make use of the method discussed earlier.
thanks,
danny
Please Log in or Create an account to join the conversation.
- Earl
-
- Offline
- New Member
- Posts: 3
However, when trying to get it to work in a module using the same code, I get the following error:
Fatal error: Using $this when not in object context in /home/totalinf/public_html/detroitrevival.org/modules/mod_k2_content/tmpl/Quotes/default.php on line 85
(The first line in the code below is line 85)
Here is the code that I am using in the default.php file here lines 83-98: /modules/mod_k2_content/tmpl/Quotes (please note that I created a sub-template):
<?php if($this->item->params->get('itemExtraFields') && count($this->item->extra_fields)): ?>
<!-- Item extra fields -->
<div class="mb-wrap mb-style-1">
<div class="mb-thumb"></div>
<blockquote class="type<?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">
<p><?php echo $this->item->extraFields->k_quote->value ?> </p>
</blockquote>
<div class="mb-attribution">
<p class="mb-author"><?php echo $this->item->extraFields->q_author->value ?> </p>
<cite><?php echo $this->item->extraFields->q_citation_source->value ?></cite>
</div>
</div>
<div class="clr"></div>
</div>
<?php endif; ?>
What I am trying to do is display the latest content (1 item) from the 'Quote' K2 category, in a module (basically ONLY the extra fields display in a module). The content is entered in the Extra Fields in the K2 item.
I've tried the solutions mentioned above but I'm not having any luck.
Thanks in advance for your wisdom :-)
Respectfully,
EJ Lear
Please Log in or Create an account to join the conversation.
- Earl
-
- Offline
- New Member
- Posts: 3
Was your suggestion and the code on your site only valid pre 2.6.2?
I still can't get this to work, very frustrating :-(
Please Log in or Create an account to join the conversation.
- Peter Grube
-
- Offline
- Elite Member
- Posts: 241
William White wrote: Code that works in the split content override below. You can also use the | (or) if you want to show more than one extra field and can specify it by name
<?php foreach ($item->extra_fields as $extraField): ?> <?php if($extraField->name == "ExtraFieldName"): ?> <li class="type<?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>"> <span class="moduleItemExtraFieldsLabel"><b><?php echo $extraField->name; ?></b></span> <span class="moduleItemExtraFieldsValue"><?php echo $extraField->value; ?></span> <br class="clr" /> </li> <?php endif;?> <?php endforeach; ?>
Thx heaps William this code works perfectly in the K2 Content mod joomla 2.5.9 & k2 2.6.5, I would like to ask 1 question as my PHP skills are very basic, I tried the OR method it seems to be working but could you let me know if I got the code right, it seemed to easy to me.
Log in or Create an account to join the conversation.
- Theofanis Valmas
-
- Offline
- New Member
- Posts: 1
<?php foreach ($items as $key=>$item): ?>
<li class="<?php echo ($key%2) ? "odd" : "even"; if(count($items)==$key+1) echo ' lastItem'; ?>">
(around line 25 in the mod_k2_content/tmpl/default.php)
<?php
if (!isset($item->extraFields))
{
$item->extraFields = new stdClass;
}
foreach ($item->extra_fields as $extraField)
{
$tmpAlias = $extraField->alias;
$item->extraFields->$tmpAlias = $extraField;
}
?>
now you can type any extra field as this:
$item->extraFields->EXTRA_FIELD_ALIAS->value
or
$item->extraFields->EXTRA_FIELD_ALIAS->name
(the code is from components/com_k2/models/item.php not mine)
this gives you the object to use with alias as you did in item.php
hope it helps!
Please Log in or Create an account to join the conversation.
- Uldis
-
Topic Author
- Offline
- Senior Member
- Posts: 69
Thank you very much, for suggesting this, it has helped me a lot !!!
Please Log in or Create an account to join the conversation.
- Tim
-
- Offline
- New Member
- Posts: 17
Log in or Create an account to join the conversation.
- Jonathan Roza
-
- Offline
- New Member
- Posts: 3
Using J.2.5.14 and K2 2.6.6.
This works for me on default.php on mod_k2_content on a template override.
=============================================
<?php
if (!isset($item->extraFields))
{
$item->extraFields = new stdClass;
}
foreach ($item->extra_fields as $extraField)
{
$tmpAlias = $extraField->alias;
$item->extraFields->$tmpAlias = $extraField;
}
?>
<div class="moduleItemExtraFields"><span class="moduleItemExtraFieldsValue"><?php echo $item->extraFields->EXTRAFIELD_ALIAS->value; ?></span>
=============================================
This is right after plugins/k2-plugins Before Display, and itemAuthorAvatar.
It was placed before title, image, everything else.
Dekari, Thank's for the help.
Please Log in or Create an account to join the conversation.