abuyoyo/cmb2-switch-button

此包的最新版本(1.2.2)没有可用的许可信息。

CMB2 切换按钮

安装: 520

依赖者: 1

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 3

类型:wordpress-plugin

1.2.2 2020-11-19 02:24 UTC

This package is auto-updated.

Last update: 2024-09-19 10:36:42 UTC


README

WordPress 的 CMB2 Metabox 的自定义切换按钮字段类型。

安装

您可以将它作为插件安装,或者将其主文件包含到您的主题或插件文件夹中。

用法

add_action( 'cmb2_admin_init', 'create_your_metabox' );
if(!function_exists('create_your_metabox')){
  function create_your_metabox(){
    $prefix = '_slug_';

    $cmb2_metabox = new_cmb2_box( array(
        'id'            => $prefix . 'test_metabox',
        'title'         => esc_html__( 'Test Metabox', 'tmv' ),
        'object_types'  => array( 'page'), // Post type
        'priority'   => 'high',
        'context'    => 'normal',
    ) );

    $cmb2_metabox->add_field( array(
        'name'             => esc_html__( 'Dynamically Load', 'text-domain' ),
        'id'               => $prefix . 'metabox_id',
        'desc'             => esc_html__('','text-domain'),
        'type'	           => 'switch',
        'default'          => true, //If it's checked by default 
        'active_value'     => true,
        'inactive_value'   => false
    ) );
  }
}
  • 如果您将 active_value 和 inactive_value 设置为布尔值,如 1/0、true/false,只需如下使用:
$test_meta = get_post_meta($post->ID, '_slug_metabox_id', true);

if($test_meta){
  //Do something when it's checked;
}
  • 如果您将 active_value 和 inactive_value 设置为除布尔值以外的特定值,例如启用/禁用,您可以使用如下方式:
$test_meta = get_post_meta($post->ID, '_slug_metabox_id', true);

if($test_meta === 'enable'){
  //Do something when it's checked;
}else{
  //Do something when it's unchecked;
}

屏幕截图

关注我们