2017-08-01 36 views
1
$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'), 
    ), 
)); 

以上是代码。现在假设如果我选择了方案二,我想执行一个逻辑等选择→深入浅出这个→如何选择选项条件?

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

选项2。我们如何在编程时做到这一点?

以上是从CMB2 Wordpress Plugin

+0

该条件应放置在哪里,某处接近$ cmb-> add_field? – PayteR

+0

没有其他地方的主题。 – somethingnow

+0

您必须为onChange事件创建一些JavaScript并使用Ajax请求调用PHP脚本。 –

回答

1

代码,如果我得到它的权利,那么你只需要从元后该值,并使条件。

$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 
}