The goal is to tie the CMB to any page that uses a particular template.
The process? Test to see if the page is using a particular template, then add the boxes. However, when I include the conditional statements, I don't get any metabox at all
The meta box doesn't display at all. However, it works when I remove the conditional statement(s).
Below is the sample code.
add_action( 'cmb2_admin_init', 'yourprefix_register_about_page_metabox' );
/**
* Hook in and add a metabox that only appears on the 'About' page
*/
function yourprefix_register_about_page_metabox() {
$prefix = 'yourprefix_about_';
/**
* Metabox to be displayed on a single page ID
*/
global $post;
$pageTemplate = get_post_meta($post->ID, '_wp_page_template', true);
if($pageTemplate == 'page-templates/template-ventures.php' ){
$cmb_about_page = new_cmb2_box( array(
'id' => $prefix . 'metabox',
'title' => esc_html__( 'About Page Metabox', 'cmb2' ),
'object_types' => array( 'page', ), // Post type
'context' => 'normal',
'priority' => 'high',
'show_names' => true // Show field names on the left
) );
$cmb_about_page->add_field( array(
'name' => esc_html__( 'Test Text', 'cmb2' ),
'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
'id' => $prefix . 'text',
'type' => 'text',
) );
}
}
I would recommend checking out https://github.com/WebDevStudios/CMB2/wiki/Display-Options#limit-to-specific-page-templates and utilize the existing parameter available by default.
Let us know if you have any issues with this method.
Thanks @tw2113 I had just spotted this method in the Wiki.
It works perfectly!
Welcome :) Glad we could help get the metaboxes conditionally showing.
Hello Guys,
I need advice, I looked for hours but still I tried all variations but still do not work.
I have two pages templates, one is Default template and another is Template 2.
What I trying to achieve is when is default template in backend there is only Title field and content field. Which don't need to be changed. But as soon as you choose 'Template 2' in back end there will be displayed extra meta boxes and fields underneath. I tried to display by page ID and all of that nothing works, it's because whichever template you choose, it displaying all the meta boxes in back end with any page template.
Because in terms of website, If I choose specific template it displays correctly, on front end. But all I need hide and display on back end too, and cannot achieve that, unless I do something wrong?
See code below I use which is was taken from https://github.com/CMB2/CMB2/wiki/Display-Options#limit-to-specific-page-templates
$cmb = new_cmb2_box( array(
'id' => 'pathway',
'title' => 'About Pathway',
'object_types' => array( 'page' ), // post type
'show_on_cb' => array( 'key' => 'archive-path', 'value' => 'archive-path.php' ),
'context' => 'normal', // 'normal', 'advanced', or 'side'
'priority' => 'high', // 'high', 'core', 'default' or 'low'
'show_names' => true, // Show field names on the left
) );
You will want to keep the "page-template" portion of this:
'show_on' => array( 'key' => 'page-template', 'value' => 'template-contact.php' ),
For your case specifically, you'll want it to read:
'show_on_cb' => array( 'key' => 'page-template', 'value' => 'archive-path.php' ),
Once that change is made, it should start working like expected.
That's absolute worked, thank you very much
should be only 'show_on' => array( 'key' => 'page-template', 'value' => 'archive-path.php' ),
without cb and all worked perfectly well! thanks
Good catch regarding the "_cb" suffix as well. That version is meant for providing a callback function/method to use, while we're just passing in a value directly.
Hi, @tw2113
Is it possible to just select to show metabox? Not click to update 馃槂
Not sure personally.
This plugin may help with that request @munkhbayaryo https://github.com/jcchavezs/cmb2-conditionals
'show_on' => array( 'key' => 'id', 'value' => array( 143,155 ) ),
I've got a big solution. Thanks @tw2113
Most helpful comment
I would recommend checking out https://github.com/WebDevStudios/CMB2/wiki/Display-Options#limit-to-specific-page-templates and utilize the existing parameter available by default.
Let us know if you have any issues with this method.