themevan/cmb2-toggle

1.0.2 2022-06-17 10:04 UTC

This package is auto-updated.

Last update: 2024-09-17 15:11:05 UTC


README

CMB2 切换字段,用于 WordPress 的 CMB2 Metabox。

截图

Screenshot

安装

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

用法

add_action( 'cmb2_admin_init', 'theme_slug_create_custom_metabox' );

function theme_slug_create_custom_metabox() {
  $prefix = '_theme_slug_';

  $cmb2_metabox = new_cmb2_box(
    array(
      'id'           => $prefix . 'sidebar_settings',
      'title'        => esc_html__( 'Sidebar Settings', 'theme-slug' ),
      'object_types' => array( 'page' ),
      'priority'     => 'high',
      'context'      => 'normal',
    )
  );

  $cmb2_metabox->add_field(
    array(
      'name'    => esc_html__( 'Enable Sidebar', 'theme-slug' ),
      'id'      => $prefix . 'enable_sidebar',
      'desc'    => esc_html__( 'Enables sidebar in the page.', 'text-domain' ),
      'type'    => 'toggle',
      'default' => 'on', // If it is checked by default.
    )
  );
}
  • 切换字段的示例用法
$enable_sidebar = get_post_meta( $post->ID, '_theme_slug_enable_sidebar', true );

if ( 'on' === $enable_sidebar ){
  // Do something when it is checked;
}

关注我们