如何选择选项条件?

时间:2017-08-01 20:18:47

标签: php wordpress if-statement syntax conditional

    $cmb->add_field( array(
    'name'             => 'Select Video or Image',
    'desc'             => 'Select an option',
    'id'               => 'the_wiki_id_one',
    'type'             => 'select',
    'show_option_none' => true,
    'default'          => 'custom',
    'options'          => array(
        'standard' => __( 'Option One', 'cmb2' ),
        'custom'   => __( 'Option Two', 'cmb2' ),
        'none'     => __( 'Option Three', 'cmb2' ),
    ),
) );

以上是代码。现在假设如果我选择了Option 2,并且我想执行一个像这样的逻辑→

If (Option 2 selected) {
 <?php Then execute some PHP code ?>
}

选择选项2→外行语言。我们怎样才能编程呢?

以上是来自CMB2 Wordpress Plugin

的代码

1 个答案:

答案 0 :(得分:1)

如果我做对了,那么你只需要从post meta获得该值,并制作条件。

$the_wiki_id_one = get_post_meta($post_id, 'the_wiki_id_one', true);

// Option 2 is selected
if( 'custom' === $the_wiki_id_one ){
 //Then execute some PHP code
}