Inventory_Presser_Taxonomies::taxonomy_meta_box_html( string $taxonomy_name, string $element_name, WP_Post $post )
Creates HTML output for a meta box that turns a taxonomy into a select drop-down list instead of the typical checkboxes. Including a blank option is the only way a user can remove the value.
Parameters Parameters
- $taxonomy_name
-
(string) (Required)
- $element_name
-
(string) (Required)
- $post
-
(WP_Post) (Required) A post
Return Return
(string) HTML that renders a editor meta box for a taxonomy
Source Source
File: includes/class-taxonomies.php
protected static function taxonomy_meta_box_html( $taxonomy_name, $element_name, $post ) {
$html = sprintf(
'<select name="%s" id="%s"><option></option>',
esc_attr( $element_name ),
esc_attr( $element_name )
);
// get all the term names and slugs for $taxonomy_name.
$terms = get_terms(
array(
'taxonomy' => $taxonomy_name,
'hide_empty' => false,
)
);
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
// get the saved term for this taxonomy.
$saved_term_slug = self::get_term_slug( $taxonomy_name, $post->ID );
foreach ( $terms as $term ) {
$html .= sprintf(
'<option value="%s"%s>%s</option>',
esc_attr( $term->slug ),
selected( strtolower( $term->slug ), strtolower( $saved_term_slug ), false ),
esc_html( $term->name )
);
}
}
return $html . '</select>';
}
Expand full source codeCollapse full source codeView on Github