0

I have a custom field that's a radio button with 3 choices (yes, no, other).

What I'd like to do is the following:

[If custom field value is yes]

Display something here

[elseIf custom field value is no]

Display something here

[elseIf custom field value is other]

[else]

The code I have thus far isn't working, here it is.

    <?php 
        global $wp_query;
        $postid = $wp_query->post->ID;                  
        $can_download = get_post_meta($postid, 'enable_downloads', true);
        if ($can_download == 'yes') { ?>

    <?php } elseif ($can_download == 'no') { ?>

    <?php } elseif ($can_download == 'other') { ?>

    <?php } else { ?>

    <?php } ?>

No matter what it's just defaulting to yes, I can't figure it out, not sure if this is even the correct way to approach.

  • How is that custom field created? plugin or hardcoded in functions.php (if yes post the code)? also do a var_dump($can_download) at the end to see the output. – Claudiu Creanga Jun 10 '15 at 0:05
  • It's created using the Advanced Custom Field plugin. – Chozen Jun 10 '15 at 0:25
1

If it's created with ACF, then the way to call the field is like this:

$can_download = get_field('enable_downloads',$postid);

so your code should look like:

<?php 
        global $wp_query;
        $postid = $wp_query->post->ID;                  
        $can_download = get_field('enable_downloads',$postid);
        if ($can_download == 'yes') { ?>

    <?php } elseif ($can_download == 'no') { ?>

    <?php } elseif ($can_download == 'other') { ?>

    <?php } else { ?>

    <?php } ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.