Support Forum
Support › Meta Box Include Exclude › Showing Metabox for only certain template.
I am trying to show a certain metabox only on a certain template. I have tried putting this code in my functions.php file:
add_filter( 'rwmb_meta_boxes', 'prefix_include_exclude_demo' );
function prefix_include_exclude_demo( $meta_boxes )
{
// 1st meta box
$meta_boxes[] = array(
'title' => 'Template - Home',
// Register this meta box for posts matched below conditions
'include' => array(
// With all conditions below, use this logical operator to combine them. Default is 'OR'. Case insensitive. Optional.
'relation' => 'OR',
// List of post IDs. Can be array or comma separated. Optional.
'ID' => array(),
// List of post parent IDs. Can be array or comma separated. Optional.
'parent' => array(),
// List of post slugs. Can be array or comma separated. Optional.
'slug' => array(),
// List of page templates. Can be array or comma separated. Optional.
'template' => array( 'page-home.php' ),
// List of categories IDs or names or slugs. Can be array or comma separated. Optional.
'category' => array(),
// List of tag IDs or names or slugs. Can be array or comma separated. Optional.
'tag' => array(),
// Custom taxonomy. Optional.
// Format: 'taxonomy' => list of term IDs or names or slugs (can be array or comma separated)
'location' => array(),
'os' => array(),
// List of parent categories IDs or names or slugs. Can be array or comma separated. Optional.
'parent_category' => 'Parent',
// List of parent tag IDs or names or slugs. Can be array or comma separated. Optional.
'parent_tag' => 'Parent',
// Parent custom taxonomy. Optional.
// Format: 'parent_taxonomy' => list of term IDs or names or slugs (can be array or comma separated)
'parent_location' => array(),
// Check if current post/page is a child page
'is_child' => true,
// List of user roles. Can be array or comma separated. Optional.
'user_role' => 'administrator',
// List of user IDs. Can be array or comma separated. Optional.
'user_id' => array(),
// Custom condition. Optional.
// Format: 'custom' => 'callback_function'
// The function will take 1 parameter which is the meta box itself
'custom' => 'manual_include',
),
'fields' => array(
array(
'name' => 'Name',
'id' => 'name',
'type' => 'text',
),
),
);
return $meta_boxes;
}
/**
* Manual check for including meta box
* You can check ANY conditions here
*
* @param $meta_box
* @return bool
*/
function prefix_include_exclude_manual_include( $meta_box )
{
if ( $meta_box['title'] == 'Include Meta Box' )
return true;
return false;
}
I also assumed when the plugin was installed you could just add it via the plugin but I do not see anywhere for the template name, I see all the areas for the post ids.
I used the Hide/Show and it works but I do have a question still. How do you use the Hide / Show plugin with a Metabox that was created using the Metabox plugin rather than having to do it through your functions.php file?
Hi,
Assuming you have a page template file called template-front-page.php
in your theme folder and you want to create a meta box only for that page template, then please change the code to:
add_filter( 'rwmb_meta_boxes', 'prefix_include_exclude_demo' );
function prefix_include_exclude_demo( $meta_boxes ) {
$meta_boxes[] = array(
'title' => 'Template - Home',
'include' => array(
'template' => array( 'template-front-home.php' ),
),
'fields' => array(
array(
'name' => 'Name',
'id' => 'name',
'type' => 'text',
),
),
);
return $meta_boxes;
}
The demo code has all possible conditions for include/exclude. You just need to remove what you don't need 🙂
Thank you very much, I meant to say this earlier but have been slammed with a project. I was able to figure it out but appreciate all the help.