问题
Does anybody know how I can change the below code for my custom taxonomy (created in the Magic Fields 2 plugin) to solve my problem. I would like it to echo the name
of the selected value rather than slug
format. e.g. I'd like to echo Client 1
and Client 2
instead of client-1
and client-2
as it currently does.
I'd like to display multiword names with spaces and correct capitalisation e.g. Joe Bloggs Associates
not joe-bloggs-associates
.
project_statistics_client
is the name of the field created in Magic Fields. The Type of the custom field is Term
dropdown and populates with the values from my custom Taxonomy called Clients
. This field is located inside of a field group that's named project_statistics
but I am not sure if the group name influences the code or not?
Also please note that all the above is in a custom post type called Projects
.
I have checked plugin help but still not sure:
Here is the code:
<?php $clients = get_field('project_statistics_client');
foreach($clients as $client){
echo '<div>' . $client . '</div>';
} ?>
回答1:
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 only provides a nice GUI to the basic WordPress taxomony feature and therefore all the WordPress taxonomy functions can be used. In particular, get_the_terms() may be used to get the categories of a post. This returns an array of objects (a post may be in multiple categories). Also, you need to extract the 'name' field from the objects.
回答2:
Okay, so after a little while I remembered that I had pasted some code from a forum post which has allowed me to populate the Term dropdown field with taxonomy/category options. I now think that this is where the problem lies and why it's outputting slug and not name? I have also decided to use categories rather than taxonomy because I would like to filter the loop of posts based on the selected values and from what I read this is easier to achieve with categories. I attach below the code I have added to functions.php because a) others may want to use it with Magic Fields but also b) in the hope somebody might know what I need to change in order to output name instead of 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;
}
}
来源:https://stackoverflow.com/questions/15097625/php-wordpress-code-echos-slug-not-term