Wordpress 关于CMB2限制的多个show_
我只是想和你一起去。据我所知,没有办法对一个盒子施加多重限制。post类型和两个内置的Wordpress 关于CMB2限制的多个show_,wordpress,meta-boxes,Wordpress,Meta Boxes,我只是想和你一起去。据我所知,没有办法对一个盒子施加多重限制。post类型和两个内置的show_on条件都有限制(根据,您可以添加。但是当涉及show_on时,您只能传递一个带有键/值对的数组。我尝试传递数组,但似乎不起作用,例如: $cmb = new_cmb2_box( array( 'id' => 'slideshow_content_box', 'title' => __( 'Slideshow conte
show_on
条件都有限制(根据,您可以添加。但是当涉及show_on
时,您只能传递一个带有键/值对的数组。我尝试传递数组,但似乎不起作用,例如:
$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,
));
我曾尝试使用过滤器上的cmb2\u show\u对其进行黑客攻击,但自定义参数似乎被删除了
有没有一种方法可以将多个show\u on
限制组合起来?我猜这应该是关闭的,但是对于任何继续观看的人,你可以通过“show\u on\u cb”添加一个函数来定义条件
您的解决方案类似于:
$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;
}
文档上有更多的show\u,但它很广泛,所以可能会让人困惑。嗨,这是一篇老文章,但你必须得到回调函数的参数(它包含一个对象函数add\u conditions($cmb)
),从中获取ID($cmb->object\u ID()
),然后获取页面模板($cmb->object\u ID());
谢谢!我必须使用PHP base name函数来获取模板的文件名:basename(get_page_template())