Several show_on restrictions for CMB2
I'm just going with the CMB2 plugin . As far as I can tell, there is no way to apply multiple constraints to a box. Restriction on message type and two built-in conditions show_on
(as per Display options and you can add custom conditionsshow_on
. But when it comes to show_on
, you can only pass one array with key / value pair. I tried passing an array of arrays and it doesn't seem to work. eg:
$cmb = new_cmb2_box( array(
'id' => 'slideshow_content_box',
'title' => __( 'Slideshow content' ),
'object_types' => array( 'page' ),
'show_on' => array(
array(
'key' => 'id',
'value' => array( 30 )
),
array(
'key' => 'page-template',
'value' => 'page_template.php'
),
),
'context' => 'normal',
'priority' => 'high',
'show_names' => true,
'closed' => false,
));
I tried to hack this with a filter cmb2_show_on
, but the custom arguments seem to be stripped away.
Is there a way to combine multiple constraints show_on
?
I guess this should be closed, but for any on-going viewers, you can add a function via show_on_cb to define conditions.
Your solution will look something like this:
$cmb = new_cmb2_box( array(
'id' => 'slideshow_content_box',
'title' => __( 'Slideshow content' ),
'object_types' => array( 'page' ),
'show_on_cb' => 'add_conditions',
'context' => 'normal',
'priority' => 'high',
'show_names' => true,
'closed' => false,
));
//Return true if page template is 'page-template' or id is 30.
function add_conditions() {
$page_template = get_page_template();
$page_id = get_the_id();
if ( $page_template === 'page-template' || $page_id === 30 ) {
return true;
}
return false;
}
There's more show_on documentation here , but it's extensive so it can be confusing.