Skip Navigation

[Resolved] Display of checkbox on front end with PHP

This support ticket is created 7 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

This topic contains 4 replies, has 2 voices.

Last updated by andrewB-7 7 years, 3 months ago.

Assisted by: Luo Yang.

Author
Posts
#561752

Hi there,

I have a checkbox field on a custom post type. The field has the ID 'author-facilitates-workshops'.

Please note that I am doing this in the correct single-[post-type] template file for the post type.

If the field is checked, then on the front end should be displayed the words "This author is available to facilitate workshops".

If it is not checked, then no value should be displayed at all.

I have tried all ways to get the php correct, but it's not displaying.

An example of how I want the code to work is hw I have used Advanced Custom Fields plugin in the past to achieve the same result. The code I used there was as follows:

<?php if( get_post_meta($post->ID, 'author_facilitate_workshops', true) ) { ?>This author is abailable to facilitate workshops. <?php } else { ?> <?php } ?>

...but I am not able to get the code right using the Toolset syntax.

Please help, if possible.

Many thanks again for your help.

Very best wishes,
Andrew.

#561805

Dear Andrew,

You can try with theTypes PHP function types_render_field() to get custom field value, see our document:
https://toolset.com/documentation/customizing-sites-using-php/functions/#checkbox
click link "Repeater attributes, User attributes, Term attributes, Usage examples"

If it is a custom checkboxes field, please check the document here:
https://toolset.com/documentation/customizing-sites-using-php/functions/#checkboxes

#563234

Hi there,

Thank you for your reply. Of those two, it is the checkbox field I am using, so the relevant link is here: https://toolset.com/documentation/customizing-sites-using-php/functions/#checkbox

I had already gone through those links and was already using the types_render_field() function in the correct template file. But I must be using it incorrectly.

To recap, I am looking for the correct code to make the following scenario work:

  • I have a checkbox field on a custom post type. The field has the slug 'author-facilitates-workshops'
  • The field is assigned the the custom post type with slug 'profauth'
  • the php for displaying the field is used in the template file called 'single-profauth.php'
  • If the field is checkbox is checked by the user when completing the CRED form, then on the website at this post should be displayed the words "This author is available to facilitate workshops".
  • If it is not checked, then no value should be displayed at all.
  • So there will need to be a check in the code first to see if the checkbox was checked. If not, then nothing should display at all. If it was checked then the text "This author is available to facilitate workshops" should be displayed.

I know that the format of code I need to use is as follows, but I cannot get the arguments correct to make it work in the way described above:

<?php echo(types_render_field( 'author-facilitates-workshops, array( 'arg1' => 'val1', 'arg2' => 'val2' ) )); ?>

If you can help me complete the code to make the scenario above work, that would be really appreciated.

Very best wishes,
Andrew.

#563449
checkbox.JPG

It depends on how do you setup the custom checkbox field "author-facilitates-workshops", for example, the option "Value to store" is number "1", see screenshot checkbox.JPG, then you can try below codes:

$author_facilitates_workshops = types_render_field("author-facilitates-workshops", array("output" => "raw"));
if($author_facilitates_workshops){
echo "This author is available to facilitate workshops";
}
#563745

Hi Luo,

Yes, that works beautifully, thank you very much!

Very best wishes,
Andrew.