Support Forum
Support › MB Custom Post Type › Use Checkbox Custom Type and When Checked Show Icon on FrontendResolved
Hello,
I'm creating a tournament registration form. One of my custom field options is gender:
Whenever one is checked I want to show an icon (image) on the frontend.
Can that be done? if so how?
Hi,
Do you mean to show an icon/image on the frontend form when creating a new tournament? Or show an icon/image on the single tournament page after creating a post?
Hello.
Yes, to your second question "Show an icon/image on the single tournament page after creating a post".
Can that be done?
Thanks
Hi,
When outputting the field value, you can check it in a condition and show an icon/image as well. For example
$value = rwmb_meta( 'boys_checkbox' );
// If field is checked.
if ( $value ) {
echo 'Your image here';
}
Get more details on the documentation https://docs.metabox.io/fields/checkbox/#template-usage
Question, I see you provided the PHP Code, how would this look using the tool in WordPress?
Thanks
Here's a screenshot of what I have - https://cln.sh/Wq5sdH
If girls is checked - I want to show a girl icon
If boys is checked - I want to show a boy icon
if both is checked - I want to show an image that has both girls and boys
Can that be done?
Hi,
It can be done if you use more conditions
$value = rwmb_meta( 'gender' );
if ( $value == 'boys' ) {
echo 'Boys image here';
}
if ( $value == 'girls' ) {
echo 'Girls image here';
}
...
Just the basic condition of PHP code, so please read more here https://www.php.net/manual/en/control-structures.if.php
I know this might be a dumb question. But where in the tool should I post this code? Here's a screenshot showing the "advance" section of the custom field - https://cln.sh/ZPKP2J
Thanks for all the help
Hi,
You need to add the code to your theme's post template file. Please read more on the documentation https://docs.metabox.io/displaying-fields/#using-code
Hmmm...I'm still not clear on where to post the php code and the link you provided really isn't helping me.
Here's a screenshot of my template directory - https://cln.sh/Ps69cc
Can you PLEASE help me get this code posted so I can check this box off?
Thanks
Hi,
You can add the code to the post template file of the theme. single.php
is a good started file to understand how Meta Box output the field value. Or you can contact your theme support to ask for adding the code to the template files.
Hmmm... I put the code in the single.php file and nothing is happening. Placed the code at the bottom of the file (starting at line 190). Can you take a look at the file and see what I'm doing wrong.
Here's a screenshot of the single.php file where I added the code.
Thanks again for all your help
Hi,
You are correct. The code works as well on my local site. Can you please add some text before the code to see if it displays on the single post page? Then make sure that you've updated the field value on the post.
Hello Support,
I created a loom video to show you what's going on my end - https://www.loom.com/share/1f7083376ba548789649a2b374a33ba4
I'm sure that's it's something I'm doing wrong. But I just don't know what.
Thanks again for all your help
Hi,
If you are using a builder to create the post template, the theme's template might be ignored by the builder functions. You can try to deactivate all plugins except Meta Box and MB Extensions to re-check the code.