• Resolved Atiqur Rahman

    (@atiqbd4ever)


    $wp_customize->add_setting('control_meta_data', array(
            'default'        => 'value2',
            'type'           => 'option',
        ));
        $wp_customize->add_control( 'example_select_box', array(
            'settings' => 'control_meta_data',
            'label'   => 'Hide Author Bio:',
            'section' => 'meta_data',
            'type'    => 'select',
            'choices'    => array(
                'value1' => 'Yes',
                'value2' => 'No',
            ),
        ));

    I want to use it so that i can hide/show my theme some info like Author Bio. Any help would be appreciated.

    Thanks
    Atiqur

Viewing 13 replies - 1 through 13 (of 13 total)
  • That code looks OK from a quick insepction. What part exactly are you having a problem with?

    Thread Starter Atiqur Rahman

    (@atiqbd4ever)

    I failed to use it. I’ve no idea how i could use it by using get_theme_mod

    <?php if ( get_theme_mod( 'control_meta_data' ) ) : ?>

    I wanted to hide/show contents according to value selected.

    Something like this:

    <?php if( get_theme_mod( 'control_meta_data', 'No') != 'Yes' ): ?>
        <p>Author bio information in here.</p>
    <?php endif; ?>
    Thread Starter Atiqur Rahman

    (@atiqbd4ever)

    Its not working.. I’ve exact this code in functions.php

    $wp_customize->add_section( 'post_control' , array(
        'title'       => __( 'Post Settings', 'themename' ),
        'priority'    => 6,
        'description' => 'Manage post contents from here.',
    ) );
    
    $wp_customize->add_setting('hide_author', array(
            'default'        => 'value2',
            'type'           => 'option',
        ));
    $wp_customize->add_control(
            'hide_author', array(
            'settings' => 'hide_author',
            'label'   => 'Hide About Bio',
            'section' => 'post_control',
            'type'    => 'select',
            'choices'    => array(
                'value1' => 'Yes',
                'value2' => 'No',
            ),
        ));

    AND, using this is nothing happened. Contents always showing.

    <?php if( get_theme_mod('hide_author', 'No') != 'Yes' ): ?>
    This is author data
    <?php endif; ?>

    The if() works on the actual value, and I should have seen that before. My fault for missing that.

    <?php if( get_theme_mod('hide_author', 'No') != 'value1' ): ?>
    This is author data
    <?php endif; ?>

    If that doesn’t seem to do anything, echo out the value that you’re getting back from get_theme_mod() and check what it’s actually retireving. That will soon show you if it’s got the correct value or not.

    Thread Starter Atiqur Rahman

    (@atiqbd4ever)

    I got the solution from radio button, select seems to be used for echoing selected data. Not working for conditioning. May be there is a way!

    There is.

    What does echo get_theme_mod( 'hide_author', No Value Set' ); output?

    That’s what you need to look at. That value will tell you exactly what you’re getting in that are. Also, try it after setting the value both ways, and see if there’s a difference.

    Thread Starter Atiqur Rahman

    (@atiqbd4ever)

    <?php echo get_theme_mod( 'hide_author', 'No Value Set' ); ?>

    output is whatever i selected that value, if “Yes” yes value if “No” no value.

    OK, so the value is being saved – but I don’t understand how you’re getting ‘yes’ and ‘no’ as values as you don’t have those values set in your choices.

    Another thing that I’ve missed though…

    $wp_customize->add_setting('hide_author', array(
            'default'        => 'value2',
            'type'           => 'option',
        ));

    The ‘type’ should be ‘theme_mod’, not ‘option’. Having it as ‘option’ means that you’d need to get the value using get_option() and not get_theme_mod().

    Thread Starter Atiqur Rahman

    (@atiqbd4ever)

    Yes, its working perfectly now, that type ‘option’ was the problem. SO my final code is…

    $wp_customize->add_setting('hide_author', array(
            'default'        => 'value2',
        ));
    $wp_customize->add_control(
            'hide_author', array(
            'settings' => 'hide_author',
            'label'   => 'Hide About Bio',
            'section' => 'post_control',
            'type'    => 'select',
            'choices'    => array(
                'value1' => 'Yes',
                'value2' => 'No',
            ),
        ));

    AND, its working fine with…

    <?php if( get_theme_mod('hide_author', 'value2') != 'value1' ): ?>
    This is author data
    <?php endif; ?>

    Thank you very much for your time and help.

    No problem. I’m glad that we got it sorted out. ??

    Hi guys

    I know this and old subject but I am just working on this and I have a small concern:

    Is there any difference if instead of coding this
    ‘value1’ => ‘Yes’,
    ‘value2’ => ‘No’,

    I code this:
    ‘Yes’ => ‘Yes’,
    ‘No’ => ‘No’,
    ‘Not Sure’ => ‘Not Sure’ //I add this options because of the space

    When I put ‘valueN’ i get in the front end ‘valueN’
    and when I use ‘Not Sure’ => ‘Not Sure’

    I have the value that I want to have, without having any issue (or control to do after getting the ‘valueN’, do you thinks is ok, I will not have any issue in the future?
    So far is working good !!

    Thank you guys

    If you get the value that you want, then it’s working correctly.

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘How to use wordpress customizer select options’ is closed to new replies.