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

Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • #30095
    dwayne@theolivermediagroup.com[email protected]
    Participant

    Hello,
    I'm creating a tournament registration form. One of my custom field options is gender:

    1. Boys
    2. Girls

    Whenever one is checked I want to show an icon (image) on the frontend.

    Can that be done? if so how?

    #30096
    Long NguyenLong Nguyen
    Moderator

    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?

    #30098
    dwayne@theolivermediagroup.com[email protected]
    Participant

    Hello.
    Yes, to your second question "Show an icon/image on the single tournament page after creating a post".

    Can that be done?

    Thanks

    #30101
    Long NguyenLong Nguyen
    Moderator

    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

    #30103
    dwayne@theolivermediagroup.com[email protected]
    Participant

    Question, I see you provided the PHP Code, how would this look using the tool in WordPress?

    Thanks

    #30105
    dwayne@theolivermediagroup.com[email protected]
    Participant

    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?

    #30140
    Long NguyenLong Nguyen
    Moderator

    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

    #30170
    dwayne@theolivermediagroup.com[email protected]
    Participant

    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

    #30189
    Long NguyenLong Nguyen
    Moderator

    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

    #30197
    dwayne@theolivermediagroup.com[email protected]
    Participant

    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

    #30207
    Long NguyenLong Nguyen
    Moderator

    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.

    #30262
    dwayne@theolivermediagroup.com[email protected]
    Participant

    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.

    https://cln.sh/D5ohzM

    Thanks again for all your help

    #30277
    Long NguyenLong Nguyen
    Moderator

    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.

    #30287
    dwayne@theolivermediagroup.com[email protected]
    Participant

    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

    #30294
    Long NguyenLong Nguyen
    Moderator

    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.

Viewing 15 posts - 1 through 15 (of 15 total)
  • You must be logged in to reply to this topic.