<?php
/**
* Author: Gemma Plank
* Author URL: https://makedo.net
* Version: 1.0
*/
/**
* Function to only show CMB fields if the specified taxonomy term is assigned to the post/page
*
* @return [bool] [if it returns true, it will allow the cmb fields to be shown]
*/
public function show_video_on_terms( $cmb ) {
// Store post/page id
$post_id = $cmb->object_id();
// Using the current post id, find all taxonomy terms assigned.
$terms = get_the_terms( (int) $post_id, 'category' );
// Create array of taxonomy term slugs we want to check against.
$slugs = array( 'term-1', 'term-2', 'term-3' );
// For every term, store the slug inside our slug array.
foreach ( $terms as $term ) {
// if any of the term slugs match the ones in our slug array then return true.
if ( in_array( $term->slug, $slugs, true ) ) {
return true;
}
}
return false;
}
/**
* Register Meta Boxes.
*/
public function register_meta_boxes() {
$field_box = new_cmb2_box(
array(
'id' => 'content_settings',
'title' => __( 'Title', 'textdomain' ),
'object_types' => array(
'post',
'page',
),
'priority' => 'high',
'context' => 'normal',
'show_names' => true,
'show_on_cb' => array( $this, 'show_video_on_terms' ),
)
);
$field_box->add_field(
array(
'name' => __( 'Field Title', 'textdomain' ),
'description' => __( 'Description', 'textdomain' ),
'id' => 'field_id',
'type' => 'wysiwyg',
)
);
}