• Resolved wpmhweb

    (@wppit)


    Hello,

    I created a custom fields with radio options that are currently available in the post editor.
    The custom field has one key name and 3 values, one for each option. Ultimately what I need is to be able to select one of the options and print what I want it to do. For example, if the custom field named RED has the value of 1 then show a color, if the custom field named RED has the value of 2 then shows a sentence and if the custom field named RED has no value then do not show anything.

    The custom field and values are already created, I just cannot figure out how to display it in my theme.

    Thanks,

Viewing 9 replies - 1 through 9 (of 9 total)
  • David Choi

    (@wpthemes777)

    This this link out:

    http://www.wpbeginner.com/wp-tutorials/wordpress-custom-fields-101-tips-tricks-and-hacks/

    and scroll down until “Using Custom Fields with Conditional Statement”.

    Thread Starter wpmhweb

    (@wppit)

    Thanks,

    But it doesn’t show me how to check for values. I have no problem showing a custom field with a key name. The problem I have is that I have 1 key e.g., CAR and CAR has 3 values 1, 2, 3 (3 options), so I need something like this:

    If custom field is CAR with value of 1 then “do this”
    If custom field is CAR with value of 2 then “do this”
    If custom field is CAR with value of 3 then “do this”

    David Choi

    (@wpthemes777)

    Yes it does, for example this is one of the sample code from that link:

    $author = get_post_meta( $post->ID, 'guest-author', true );
    if ( $author ) $name = $author;

    You just have to use get_post_meta function:

    http://codex.wordpress.org/Function_Reference/get_post_meta

    In your case, will probably look something like

    $CAR = get_post_meta( $post->ID, 'car', true );
    if ( $CAR == 1 ) {
        ...do something ...
    }
    elseif ( $CAR == 2 ) {
        ...do another thing...
    }
    Thread Starter wpmhweb

    (@wppit)

    Hi,
    Thank you again, from the previous link to the codex page I was able to build this:

    <?php $key_1_value = get_post_meta($post->ID, 'wpcf-huge-image', true); ?>
    
    <?php if($key_1_value != '1') { ?>
                       hello 1
     <?php } elseif($key_1_value != '2') { ?>
                       hello 2
                    <?php } ?>

    To create a custom field I am using the Types plugin and I set option #1 with the value of ‘1’ and option #2 with the value ‘2’ and you can see in the $key_1_value variable above and it works somehow, the problem I am having now is that it works backwards. When I set from the editor the option #1 it display “hello 2” and when I set the option #2 it displays “hello 1”.

    Can you figure out what I am doing wrong?

    Thank you so much.

    PS: I am still new writing conditional statements.

    David Choi

    (@wpthemes777)

    In stead of

    $key_1_value != '1'

    needs to be

    $key_1_value == '1'

    Thread Starter wpmhweb

    (@wppit)

    Thank you, it worked like a charm!

    But this is my question how did you figured it out? I am interesting on learning, do you know some online resources I can use besides Codex?

    Thanks again.

    David Choi

    (@wpthemes777)

    I am programmer, so was able to find it at a glance. The best resource is actually Google, when searching for key words, enclose it with quotes like:

    “wordpress bla bla bla”

    Because there is no one stop resource available on the internet. And Youtube.com is also a very good resource for video tutors.

    Thread Starter wpmhweb

    (@wppit)

    Appreciate the help!

    David Choi

    (@wpthemes777)

    You are welcome, have a nice day!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Custom fields with radio options, how to display content per value?’ is closed to new replies.