-
Notifications
You must be signed in to change notification settings - Fork 571
Display Options
Table of Contents generated with DocToc
- Limit to specific post types
- Limit to specific IDs
- Limit to specific page templates
- More Show On Filters
Metaboxes should be context-specific, and only show up when relevant. There's two tools you can use to limit their display:
- object_types - limits the post types it applies to
- show_on - can limit to specific page/post IDs, page templates, or any other show_on filter you define
Both of these are used when you define your metabox.
For every metabox you create, you should specify the post types to which it applies. They'll be listed as an array. Here's an example that only applies to pages:
$cmb = new_cmb2_box( array(
'id' => 'test_metabox',
'title' => 'Test Metabox',
'object_types' => array( 'page' ), // post type
'context' => 'normal', // 'normal', 'advanced', or 'side'
'priority' => 'high', // 'high', 'core', 'default' or 'low'
'show_names' => true, // Show field names on the left
) );
Here's an example that displays on posts and events:
$cmb = new_cmb2_box( array(
'id' => 'test_metabox',
'title' => 'Test Metabox',
'object_types' => array( 'post', 'event' ), // post type
'context' => 'normal', // 'normal', 'advanced', or 'side'
'priority' => 'high', // 'high', 'core', 'default' or 'low'
'show_names' => true, // Show field names on the left
) );
Let's say you have two pages, About Us (page ID - 50) and Contact Us (page ID - 24). You created a Contact Information metabox that you want only displaying on these two pages. Here's what the beginning of your metabox might look like.
$cmb = new_cmb2_box( array(
'id' => 'contact-information',
'title' => 'Contact Information',
'object_types' => array( 'page' ), // post type
'show_on' => array( 'key' => 'id', 'value' => array( 50, 24 ) ),
'context' => 'normal', // 'normal', 'advanced', or 'side'
'priority' => 'high', // 'high', 'core', 'default' or 'low'
'show_names' => true, // Show field names on the left
) );
The type of show_on filter (key) is "id" and the value for that filter is an array of your IDs. If you only wanted it on the About page you could use 'value' => 50 instead of putting it in an array.
This will limit it to the page template with the file name template-contact.php
. If you want to include it on multiple page templates, put them all in an array like in the above example.
$cmb = new_cmb2_box( array(
'id' => 'contact-information',
'title' => 'Contact Information',
'object_types' => array( 'page' ), // post type
'show_on' => array( 'key' => 'page-template', 'value' => 'template-contact.php' ),
'context' => 'normal', // 'normal', 'advanced', or 'side'
'priority' => 'high', // 'high', 'core', 'default' or 'low'
'show_names' => true, // Show field names on the left
) );
You can also Add your own show_on filters, and that page lists some examples.
Documentation:
-
Installation
Installation instructions for various environments -
Basic Usage
Get up and running -
Advanced Usage
Not for the faint of heart -
Field Types
Breakdown of field types -
Examples
Examples for registering fields -
Field Parameters
Breakdown of common field parameters -
Display Options
Limit display of boxes -
Box Properties
Breakdown of box properties -
Troubleshooting
Common issues & how to deal with them -
Notable Changes in CMB2 (from original CMB)
CMB2 — a complete re-write -
Tips & Tricks
10x your CMB2 skills -
REST API
CMB2 data through WordPress REST API -
Javascript API
Work with CMB2 JS events & hooks
Advanced tutorials:
- Adding your own field types
- Adding your own show_on filters
- Displaying boxes on the front-end
- Using CMB to create an Admin Theme Options Page
- Create New Posts (or Custom Post Type) Entries Using A Front End Form
- Disable CMB2 Styles on Front End Forms
External Resources: