Created
January 3, 2012 12:14
-
-
Save zanematthew/1554712 to your computer and use it in GitHub Desktop.
Builds a option list of Custom Taxonomies for use in WordPress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Build an option list of Terms based on a given Taxonomy. | |
* | |
* @package helper | |
* @uses zm_base_get_terms to return the terms with error checking | |
* @param string $taxonomy | |
* @param mixed $value, the value to be used in the form field, can be term_id or term_slug | |
*/ | |
if ( ! function_exists( 'zm_base_build_options' ) ) : | |
function zm_base_build_options( $taxonomy=null, $value=null ) { | |
if ( is_null ( $value ) ) | |
$value = 'term_id'; | |
if ( is_array( $taxonomy ) ) | |
extract( $taxonomy ); | |
// white list | |
if ( empty( $prepend ) ) | |
$prepend = null; | |
if ( empty( $extra_data ) ) | |
$extra_data = null; | |
if ( empty( $extra_css ) ) | |
$extra_css = null; | |
if ( $multiple ) { | |
$multiple = 'multiple="multiple"'; | |
$multiple_extra = '[]'; | |
} else { | |
$multiple = false; | |
} | |
if ( !isset( $label ) ) | |
$label = $taxonomy; | |
/** All Terms */ | |
$args = array( | |
'orderby' => 'name', | |
'hide_empty' => false | |
); | |
$terms = get_terms( $taxonomy, $args ); | |
if ( is_wp_error( $terms ) ) { | |
// exit( "Opps..." . $terms->get_error_message() . "..dog, cmon, fix it!" ); | |
$terms = false; | |
} | |
global $post; | |
if ( $post ) { | |
$current_terms = get_the_terms( $post->ID, $taxonomy ); | |
} | |
?> | |
<?php if ( $terms ) : ?> | |
<fieldset class="zm-base-<?php echo $taxonomy; ?>-container <?php echo $taxonomy; ?>-container"> | |
<label class="zm-base-title"><?php echo $label; ?></label> | |
<select name="<?php echo $taxonomy; ?><?php echo $multiple_extra;?>" <?php echo $multiple; ?> <?php echo $extra_data; ?> class="<?php echo $extra_class; ?>" id="" <?php echo $multiple; ?>> | |
<?php // First choice ?> | |
<option value="">-- Choose a <?php echo $taxonomy; ?> --</option> | |
<?php foreach( $terms as $term ) : ?> | |
<?php for ( $i=0, $count=count($current_terms); $i <= $count; $i++ ) : ?> | |
<?php // yes fuckig cryptic, i hate for's in foreach's; ?> | |
<?php $current_terms[$term->term_id]->name ? $temp = $current_terms[$term->term_id]->name : $temp = null; ?> | |
<?php endfor; ?> | |
<?php $term->name == $temp ? $selected = 'selected="selected"' : $selected = null; ?> | |
<option | |
value="<?php echo $prepend; ?><?php echo $term->$value; ?>" | |
data-value="<?php echo $term->slug; ?>" | |
class="taxonomy-<?php echo $taxonomy; ?> term-<?php echo $term->slug; ?> <?php echo $taxonomy; ?>-<?php echo $term->term_id; ?>" | |
<?php echo $selected; ?>> | |
<?php echo $term->name; ?> | |
</option> | |
<?php endforeach; ?> | |
</select> | |
</fieldset> | |
<?php endif; ?> | |
<?php } | |
endif; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment