这里有一个函数,您可以传递单个post id或id数组。在添加元框的函数中调用此函数。如果id或id不匹配,元框将不会显示在该帖子或页面上。
function check_id( $id ) {
// Get the current post ID
if ( isset( $_GET[ \'post\' ] ) ) $post_id = $_GET[ \'post\' ];
elseif ( isset( $_POST[ \'post_ID\' ] ) ) $post_id = $_POST[ \'post_ID\' ];
if ( ! isset( $post_id ) )
return false;
// If value isn\'t an array, turn it into one
$id = ! is_array ( $id ) ? array ( $id ) : $id;
// If current page id is in the included array, display the metabox
if ( in_array ( $post_id, $id ) )
return true;
else
return false;
}
function myplugin_add_custom_box() {
$what_page = check_id( array( 3, 18, 15 ) );
if ( false == $what_page ) return;
add_meta_box(
\'myplugin_sectionid\',
__( \'My Post Section Title\', \'myplugin_textdomain\' ),
\'myplugin_inner_custom_box\',
\'post\'
);
add_meta_box(
\'myplugin_sectionid\',
__( \'My Post Section Title\', \'myplugin_textdomain\' ),
\'myplugin_inner_custom_box\',
\'page\'
);
}