有人知道我如何为我的自定义分类法(在Magic Fields 2插件中创建)更改下面的代码来解决我的问题吗?我希望它响应所选值的name
而不是slug
格式。我想回显Client 1
和Client 2
,而不是现在的client-1
和client-2
。
我想显示带有空格和正确大写的多字名称,例如Joe Bloggs Associates
而不是joe-bloggs-associates
。
project_statistics_client
是在Magic Fields中创建的字段名。自定义字段的类型是Term
下拉列表,并填充来自自定义分类法Clients
的值。这个字段位于一个名为project_statistics
的字段组内,但我不确定组名是否影响代码?
还请注意,以上所有内容都是在一个名为Projects
的自定义帖子类型中。
我已经检查了插件帮助,但仍然不确定:
代码如下:
<?php $clients = get_field('project_statistics_client');
foreach($clients as $client){
echo '<div>' . $client . '</div>';
} ?>
global $post;
$taxonomy = 'your taxonomy';
$terms = get_the_terms($post->ID, $taxonomy);
if ( is_array($terms) ) {
echo(implode(',', wp_list_pluck($terms, 'name')));
}
Magic Fields 2仅为基本的WordPress分类法功能提供了一个不错的GUI,因此可以使用所有WordPress分类法功能。特别是,get_the_terms()可用于获取帖子的类别。这将返回一个对象数组(一篇文章可能属于多个类别)。此外,您需要从对象中提取'name'字段。
好了,过了一会儿,我想起来我已经从一个论坛帖子中粘贴了一些代码,这些代码允许我用分类法/类别选项填充Term下拉字段。我现在认为这就是问题所在,为什么它输出的是slug而不是name?我还决定使用类别而不是分类法,因为我想根据所选值过滤帖子的循环,从我读到的情况来看,使用类别更容易实现这一点。我附上下面的代码,我已经添加到函数。php,因为a)其他人可能想要使用它与魔术领域,但也b)希望有人可能知道我需要改变,以输出名称而不是塞格?
/**
* 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;
}
}