Skip to content

Instantly share code, notes, and snippets.

@monecchi
Forked from mustardBees/example.php
Last active November 17, 2016 13:27
Show Gist options
  • Save monecchi/cd8d3e92c0795535f590be4a2d0289f9 to your computer and use it in GitHub Desktop.
Save monecchi/cd8d3e92c0795535f590be4a2d0289f9 to your computer and use it in GitHub Desktop.
<?php
// Example meta box field
$featured_categories->add_field( array(
'name' => __( 'Featured categories', 'iweb' ),
'id' => 'featured_categories',
'type' => 'pw_multiselect',
'options' => iweb_get_cmb_options_array_tax( 'category' ),
) );
/**
* Get a list of terms
*
* Generic function to return an array of taxonomy terms formatted for CMB2.
* Simply pass in your get_terms arguments and get back a beautifully formatted
* CMB2 options array.
*
* @param string|array $taxonomies Taxonomy name or list of Taxonomy names
* @param array|string $query_args Optional. Array or string of arguments to get terms
* @return array CMB2 options array
*/
function iweb_get_cmb_options_array_tax( $taxonomies, $query_args = '' ) {
$defaults = array(
'hide_empty' => false
);
$args = wp_parse_args( $query_args, $defaults );
$terms = get_terms( $taxonomies, $args );
$terms_array = array();
if ( ! empty( $terms ) ) {
foreach ( $terms as $term ) {
$terms_array[$term->term_id] = $term->name;
}
}
return $terms_array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment