如果我转到wp管理/编辑标签。php?taxonomy=位置然后我正确地看到了术语列表。
如果我循环通过:
get_terms("category");
然后,我正确地看到了类别分类的几个术语。我是否为同一个函数错误地创建了自定义分类法,而没有为我的分类法输出任何结果?
get_terms("location");
register_taxonomy(\'location\', \'post\',
array(
\'labels\' => array( \'name\' => _x( \'Locations\',
\'taxonomy general name\' ),
\'singular_name\' => _x( \'Location\', \'taxonomy singular name\' ),
\'search_items\' => __( \'Search Locations\' ),
\'all_items\' => __( \'All Locations\' ),
\'parent_item\' => __( \'Parent Location\' ),
\'parent_item_colon\' => __( \'Parent Location:\' ),
\'edit_item\' => __( \'Edit Location\' ),
\'update_item\' => __( \'Update Location\' ),
\'add_new_item\' => __( \'Add New Location\' ),
\'new_item_name\' => __( \'New Location Name\' ),
\'menu_name\' => __( \'Locations\' ),
),
\'rewrite\' => array( \'slug\' => \'locations\',
\'with_front\' => false,
\'hierarchical\' => true
),
)
);
SO网友:samjco
尝试使用WP_Term_Query
:
从此处获取所有参数:https://developer.wordpress.org/reference/classes/WP_Term_Query/__construct/
$term_query = new WP_Term_Query( array(
\'taxonomy\' => \'regions\', // <-- Custom Taxonomy name..
\'orderby\' => \'name\',
\'order\' => \'ASC\',
\'child_of\' => 0,
\'parent\' => 0,
\'fields\' => \'all\',
\'hide_empty\' => false,
) );
// Show Array info
echo "<pre>";
print_r($term_query->terms);
echo "</pre>";
//Render html
if ( ! empty( $term_query->terms ) ) {
foreach ( $term_query ->terms as $term ) {
echo $term->name .", ";
echo $term->term_id .", ";
echo $term->slug .", ";
echo "<br>";
}
} else {
echo \'‘No term found.’\';
}