好的,过了一会儿我记得我从论坛帖子中粘贴了一些代码,这使我可以使用分类/类别选项填充术语下拉字段。我现在认为这就是问题所在,为什么它输出的是 slug 而不是名称?我还决定使用类别而不是分类,因为我想根据所选值过滤帖子循环,并且从我读到的内容来看,使用类别更容易实现。我附在我添加到functions.php的代码下方,因为a)其他人可能想将它与Magic Fields一起使用,但也b)希望有人可能知道我需要更改什么才能输出名称而不是slug?
/**
* Custom Taxonomy Dropdown
*/
// initialisation
global $mf_domain;
// class with static properties encapsulating functions for the field type
class term_field extends mf_custom_fields {
public $allow_multiple = TRUE;
public $has_properties = TRUE;
public function _update_description(){
global $mf_domain;
$this->description = __("This field allows to do relations with taxonomie terms",$mf_domain);
}
public function _options(){
global $mf_domain;
// Get the taxonomie as dropdownoption
$select = array();
$tax = get_taxonomies();
foreach($tax as $k => $v){
$select[] = $v;
}
$data = array(
'option' => array(
'term' => array(
'type' => 'select',
'id' => 'term',
'label' => __('related taxonomy: ',$mf_domain),
'name' => 'mf_field[option][term]',
'default' => '',
'options' => $select,
'add_empty' => false,
'description' => '',
'value' => '',
'div_class' => '',
'class' => ''
),
)
);
return $data;
}
public function display_field( $field, $group_index = 1, $field_index = 1 ) {
global $mf_domain;
// If is not required this field be added a None value
$notype = "";
if( !$field['required_field'] ) {
$notype = ( !empty($field['options']['notype']) ) ? $field['options']['notype'] : __( "-- None --" , $mf_domain );
}
$output = '';
// Get the taxonomie as dropdownoption
$select = array();
$tax = get_taxonomies();
foreach($tax as $k => $v){
$select[] = $v;
}
$option_from_term_array = $field['options']['term'];
$options = get_terms($select[$option_from_term_array], array('hide_empty' => false));
$output = '<div class="mf-dropdown-box">';
$value = $field['input_value'];
$output .= sprintf('<select class="dropdown_mf" id="%s" name="%s" >',$field['input_id'],$field['input_name']);
if( $notype != "" ) {
$output .= "<option value=''>$notype</option>";
}
foreach($options as $option) {
$check = ($option->slug == $value) ? 'selected="selected"' : '';
$output .= sprintf('<option value="%s" %s >%s</option>',
esc_attr($option->slug),
$check,
esc_attr($option->name)
);
}
$output .= '</select>';
$output .= '</div>';
return $output;
}
}