如何在wordpress中显示分类标准值?
问题描述:
我注册了一个分类:如何在wordpress中显示分类标准值?
<?php function event_cities_init() {
register_taxonomy('event-cities', array('events'), array(
'hierarchical' => false,
'public' => false,
'show_in_nav_menus' => false,
'show_ui' => true,
'show_admin_column' => false,
'query_var' => true,
'rewrite' => array('slug' => 'city'),
'capabilities' => array(
'manage_terms' => 'edit_posts',
'edit_terms' => 'edit_posts',
'delete_terms' => 'edit_posts',
'assign_terms' => 'edit_posts'
),
'labels' => array(
'name' => __('City', 'projecttitle'),
'singular_name' => _x('City', 'taxonomy general name', 'projecttitle'),
'search_items' => __('Search Cities', 'projecttitle'),
'popular_items' => __('Popular Cities', 'projecttitle'),
'all_items' => __('All Cities', 'projecttitle'),
'parent_item' => __('Parent City', 'projecttitle'),
'parent_item_colon' => __('Parent City:', 'projecttitle'),
'edit_item' => __('Edit City', 'projecttitle'),
'update_item' => __('Update City', 'projecttitle'),
'add_new_item' => __('New City', 'projecttitle'),
'new_item_name' => __('New City', 'projecttitle'),
'separate_items_with_commas' => __('Separate Cities with commas', 'projecttitle'),
'add_or_remove_items' => __('Add or remove Cities', 'projecttitle'),
'choose_from_most_used' => __('Choose from the most used Cities', 'projecttitle'),
'not_found' => __('No Cities found.', 'projecttitle'),
'menu_name' => __('City', 'projecttitle'),
),
'show_in_rest' => true,
'rest_base' => 'event-cities',
'rest_controller_class' => 'WP_REST_Terms_Controller',
));}add_action('init', 'event_cities_init');
我怎么能简单地显示了一定的分类的“名称”? 我试过下面的代码,它打印出一个大数组,但我只想要它的名字,我无法捕捉它。
$terms = get_terms('event-types');
var_dump($terms);
非常感谢。
答
get_terms()函数返回指定分类的所有项。您可以使用foreach循环就像下面通过您的条款迭代,并使用术语对象的名称属性来访问条款名称:
$terms = get_terms('event-types');
foreach($terms as $term) {
echo $term->name;
}
你可以得到特定术语如下
get_term_by($field, $value, $taxonomy)
例如
get_term_by('id', '19', 'event-types')
你可以得到分类名称与此代码:'get_taxonomy( '事件类型') - > labels-> name' –
好吧,也许我还不够精确。我读过(https://codex.wordpress.org/Function_Reference/get_taxonomy),它没有返回与分类相关的术语列表。但这就是我想要的。那么我如何正确使用get_term()函数呢? –
是 'get_term_by('id'*,'name','event-cities');' 正确的方法? *我可以在哪里找到ID?! –