admin管理员组文章数量:1406048
I am using CMB2 for metabox on custom posts. I am adding a metabox by using code below:
$cmb_demo->add_field( array(
'name' => __( 'Test Text', 'cmb2' ),
'desc' => __( 'field description (optional)', 'cmb2' ),
'id' => $prefix . 'text',
'type' => 'text',
'show_on_cb' => 'show_this_field_if_true',
) );
I understand show_this_field_if_true will be a function that will return true or false. But, I want to make this as conditional with another field. This field will show if another field's value is true.
Here is an example that Don't show this field if it's not the front page template
function show_this_field_if_true( $cmb ) {
if ( $cmb->object_id !== get_option( 'page_on_front' ) ) {
return false;
}
return true;
}
How can I make this conditional with a field?
I am using CMB2 for metabox on custom posts. I am adding a metabox by using code below:
$cmb_demo->add_field( array(
'name' => __( 'Test Text', 'cmb2' ),
'desc' => __( 'field description (optional)', 'cmb2' ),
'id' => $prefix . 'text',
'type' => 'text',
'show_on_cb' => 'show_this_field_if_true',
) );
I understand show_this_field_if_true will be a function that will return true or false. But, I want to make this as conditional with another field. This field will show if another field's value is true.
Here is an example that Don't show this field if it's not the front page template
function show_this_field_if_true( $cmb ) {
if ( $cmb->object_id !== get_option( 'page_on_front' ) ) {
return false;
}
return true;
}
How can I make this conditional with a field?
Share Improve this question edited Sep 25, 2015 at 16:53 Travis Northcutt 3,4005 gold badges43 silver badges60 bronze badges asked Sep 22, 2015 at 3:03 Rasel AhmedRasel Ahmed 1851 gold badge3 silver badges9 bronze badges2 Answers
Reset to default 0The best way to do this is by using JavaScript and there is a couple of CMB2 plugins let you do that easily:
- CMB2 Conditional
- CMB2 Conditional Logic
You need to replace the get_option
call with a call to get_post_meta
:
function show_this_field_if_true( $cmb ) {
// Check if other meta value exists
if ( ! get_post_meta( $cmb->object_id, 'other_meta_key_to_check', 1 ) ) {
return false;
}
return true;
}
Keep in mind, this will only work for the initial page-load and will not show the field until you update the other_meta_key_to_check
value and save the page.
本文标签: pluginsCMB2 metabox conditional logic
版权声明:本文标题:plugins - CMB2 metabox conditional logic 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744964326a2634849.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论