Skip Navigation

[Resolved] Conditional on PHP shortcode

This thread is resolved. Here is a description of the problem and solution.

Problem:
Conditional display based on custom field value using PHP

Solution:
You can use the Types PHP API function types_render_field() to get value of custom field.

You can find proposed solution in this case with the following reply:
https://toolset.com/forums/topic/conditional-on-php-shortcode/#post-2314245

Relevant Documentation:
- https://toolset.com/documentation/customizing-sites-using-php/functions/
- https://toolset.com/documentation/customizing-sites-using-php/

This support ticket is created 3 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 5 replies, has 2 voices.

Last updated by Paulo Ramalho 3 years, 3 months ago.

Assisted by: Minesh.

Author
Posts
#2314219

Hi all.
I have a Crew CPT and on the single member page I have some custom fields for the RRSS links.

" target="_blank">

I'm trying to put here some conditional when the field is empty the icon do not appear on front end.
I've searching the documentation but not find it.

Thanks in advance.

#2314245

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

You can use the Types PHP API function types_render_field() to get value of custom field.

<?php 

global $post;

$value = types_render_field('twitter',array('output'=>'raw')); 
if(!empty($value)) {

?>
<a href="<?php echo do_shortcode('[types field="twitter" output="raw"][/types]' ); ?>" target="_blank"><i class="equipo fa fa-twitter" aria-hidden="true">
</a>
<?php 
}
?>

You can adjust the above code as required.

More info:
- https://toolset.com/documentation/customizing-sites-using-php/functions/
- https://toolset.com/documentation/customizing-sites-using-php/

#2316117

Hi Minesh.
Great, It works very well. Thank you very much man.
Now I just have to figuer out how to put another fields in the same code, to not repeat the entire code for different fields.
Thanks again.

#2316123

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

If your goal is to not the display the field with link if it does not contain any value then you have to repeate that code and replace the custom field names where applicable.

#2316637

Yes, I've done it. But I'm seeing how to use less code. My PHP knowledgment is not so good.
Thanks a lot Minesh, the problem is resolved.

#2316641

Minesh was very very effective. Thanks again. My issue is resolved now. Thank you!