Use Checkbox Custom Type and When Checked Show Icon on Frontend
Support › MB Custom Post Type › Use Checkbox Custom Type and When Checked Show Icon on FrontendResolved
- This topic has 14 replies, 2 voices, and was last updated 3 years, 8 months ago by
Long Nguyen.
-
AuthorPosts
-
August 10, 2021 at 11:51 AM #30095
[email protected]
ParticipantHello,
I'm creating a tournament registration form. One of my custom field options is gender:- Boys
- Girls
Whenever one is checked I want to show an icon (image) on the frontend.
Can that be done? if so how?
August 10, 2021 at 12:34 PM #30096Long Nguyen
ModeratorHi,
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?
August 10, 2021 at 7:58 PM #30098[email protected]
ParticipantHello.
Yes, to your second question "Show an icon/image on the single tournament page after creating a post".Can that be done?
Thanks
August 10, 2021 at 8:49 PM #30101Long Nguyen
ModeratorHi,
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
August 10, 2021 at 10:11 PM #30103[email protected]
ParticipantQuestion, I see you provided the PHP Code, how would this look using the tool in WordPress?
Thanks
August 10, 2021 at 10:19 PM #30105[email protected]
ParticipantHere'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 boysCan that be done?
August 11, 2021 at 12:55 PM #30140Long Nguyen
ModeratorHi,
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
August 12, 2021 at 8:08 AM #30170[email protected]
ParticipantI 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
August 12, 2021 at 2:13 PM #30189Long Nguyen
ModeratorHi,
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
August 12, 2021 at 9:52 PM #30197[email protected]
ParticipantHmmm...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
August 13, 2021 at 10:48 AM #30207Long Nguyen
ModeratorHi,
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.August 15, 2021 at 2:50 AM #30262[email protected]
ParticipantHmmm... 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
August 16, 2021 at 10:01 AM #30277Long Nguyen
ModeratorHi,
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.
August 16, 2021 at 7:54 PM #30287[email protected]
ParticipantHello Support,
I created a loom video to show you what's going on my end - https://www.loom.com/share/1f7083376ba548789649a2b374a33ba4I'm sure that's it's something I'm doing wrong. But I just don't know what.
Thanks again for all your help
August 16, 2021 at 11:17 PM #30294Long Nguyen
ModeratorHi,
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.
-
AuthorPosts
- You must be logged in to reply to this topic.