Skip to content

Instantly share code, notes, and snippets.

@mustardBees
Created August 16, 2014 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mustardBees/40e64d44060d8718c2d0 to your computer and use it in GitHub Desktop.
Save mustardBees/40e64d44060d8718c2d0 to your computer and use it in GitHub Desktop.
<?php
/**
* Gets a number of terms and displays them as options
*
* Modified from CMB example code to save the term ID instead of slug.
*
* @link http://link.from.pw/1sLyqqv
* @param string $taxonomy Taxonomy terms to retrieve. Default is category
* @param string|array $args Optional. Change the defaults retrieving terms
* @return array An array of options that matches the CMB options array
*/
function pw_cmb_get_term_options( $taxonomy = 'category', $args = array() ) {
$args['taxonomy'] = $taxonomy;
$args = wp_parse_args( $args, array( 'taxonomy' => 'category' ) );
$taxonomy = $args['taxonomy'];
$terms = (array) get_terms( $taxonomy, $args );
// Initate an empty array
$term_options = array();
if ( ! empty( $terms ) ) {
foreach ( $terms as $term ) {
$term_options[ $term->term_id ] = $term->name;
}
}
return $term_options;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment