- Posts: 58
COMMUNITY FORUM
SOLVED: Hide extra field if empty?
- Johann Scharfetter
-
Topic Author
- Offline
- Senior Member
Less
More
12 years 7 months ago - 12 years 7 months ago #103677
by Johann Scharfetter
SOLVED: Hide extra field if empty? was created by Johann Scharfetter
Hello!
I was looking for hours to find a solution without success. I hope you can help me.
I have created some extra fields and I'm able do display them in item.php with the following code:
After <!-- Plugins: AfterDisplayTitle --> I pasted this code
I was looking for hours to find a solution without success. I hope you can help me.
I have created some extra fields and I'm able do display them in item.php with the following code:
After <!-- Plugins: AfterDisplayTitle --> I pasted this code
If the extra field is empty it is not dispayed. So far every thing is ok.
But in my case I need something more.
I have some extra code conected to <?php echo $extrafields; ?>
<div class="xyz"><?php echo $extrafields; ?></div>
or
<p><?php echo $extrafields; ?></p>
How can I hide the code like <div></div>, <p></p>, {slider=xxx}...{/slider} et cetera ... if the extra field is empty?
if $extraField is empty display '' ... els <div class="xyz"> <?php echo $extrafields; ?> </div> .. endif
Thank you!
Please Log in or Create an account to join the conversation.
- Julian
-
- Offline
- New Member
Less
More
- Posts: 10
12 years 7 months ago - 12 years 7 months ago #103678
by Julian
Replied by Julian on topic Re: Hide extra field if empty?
Hi scharfet,
this is not a ready-to-use solution, but maybe a good approach. You can check whether the variable is set and empty or not by using the PHP functions isset() and empty().
This will check if $extrafields is set and not empty:
isset()
empty()
array_key_exists()
I hope that I have understood correctly what your problem is.
Best regards,
Julian
this is not a ready-to-use solution, but maybe a good approach. You can check whether the variable is set and empty or not by using the PHP functions isset() and empty().
This will check if $extrafields is set and not empty:
Best regards,
Julian
Please Log in or Create an account to join the conversation.
- Johann Scharfetter
-
Topic Author
- Offline
- Senior Member
Less
More
- Posts: 58
12 years 7 months ago - 12 years 7 months ago #103679
by Johann Scharfetter
Replied by Johann Scharfetter on topic SOLVED: Re: Hide extra field if empty?
Hi Julian,
it works!
Thank you very much!! You helped me a lot!
Here an example for all those who are not so good at php, like me. ;)
it works!
Thank you very much!! You helped me a lot!
Here an example for all those who are not so good at php, like me. ;)
Log in or Create an account to join the conversation.
- Julian
-
- Offline
- New Member
Less
More
- Posts: 10
12 years 7 months ago #103680
by Julian
Replied by Julian on topic Re: SOLVED: Re: Hide extra field if empty?
You're welcome!
Please Log in or Create an account to join the conversation.
- Steven Johnson
-
- Offline
- Senior Member
Less
More
- Posts: 69
12 years 3 months ago #103681
by Steven Johnson
Replied by Steven Johnson on topic Re: SOLVED: Hide extra field if empty?
Thanks for the thread. I had the same need.
My issue is when the extra field is an image. Image fields will display the alt text even when their is no image, which makes the field not emply.
Any suggestions on how to write the php code to not display anything when using an image field.
Thanks! -- Steven
My issue is when the extra field is an image. Image fields will display the alt text even when their is no image, which makes the field not emply.
Any suggestions on how to write the php code to not display anything when using an image field.
Thanks! -- Steven
Please Log in or Create an account to join the conversation.
- Steven Johnson
-
- Offline
- Senior Member
Less
More
- Posts: 69
12 years 3 months ago #103682
by Steven Johnson
Replied by Steven Johnson on topic Re: SOLVED: Hide extra field if empty?
After some trial and error I came up with this line
Log in or Create an account to join the conversation.
- Steven Johnson
-
- Offline
- Senior Member
Less
More
- Posts: 69
12 years 3 months ago #103683
by Steven Johnson
Replied by Steven Johnson on topic Re: SOLVED: Hide extra field if empty?
Just updated to K2 2.6.3 and the issue is resolved. All is working correctly now.
Thanks!
Thanks!
Please Log in or Create an account to join the conversation.
- Jack Hughes
-
- Offline
- Junior Member
Less
More
- Posts: 29
12 years 3 months ago #103684
by Jack Hughes
Replied by Jack Hughes on topic Re: SOLVED: Hide extra field if empty?
Hey folks I worked this out for the new way K2 2.6.2 handles emptyfields so nothing shows if the extrafield in question is empty.
Just swap your extrafield alias with EXTRAFIELDALIASHERE and you're good
<?php if(isset($this->item->extraFields->EXTRAFIELDALIASHERE->value) === true && empty($this->item->extraFields->EXTRAFIELDALIASHERE->value) === false): ?>
<span class="itemExtraFieldsLabel"><?php echo $this->item->extraFields->EXTRAFIELDALIASHERE->name; ?>:</span>
<span class="itemExtraFieldsValue"><?php echo $this->item->extraFields->EXTRAFIELDALIASHERE->value; ?></span>
<div class="clr"></div>
<?php endif; ?>
Just swap your extrafield alias with EXTRAFIELDALIASHERE and you're good
Please Log in or Create an account to join the conversation.