我正在尝试显示子类别链接和名称,而不是父类别。有没有办法做到这一点?
function get_category() {
$buffy = \'\';
$selected_category_obj = \'\';
$selected_category_obj_id = \'\';
$selected_category_obj_name = \'\';
$current_post_type = get_post_type($this->post->ID);
$builtin_post_types = get_post_types(array(\'_builtin\' => true));
if (array_key_exists($current_post_type, $builtin_post_types)) {
// default post type
//read the post meta to get the custom primary category
$td_post_theme_settings = td_util::get_post_meta_array($this->post->ID, \'td_post_theme_settings\');
if (!empty($td_post_theme_settings[\'td_primary_cat\'])) {
//we have a custom category selected
$selected_category_obj = get_category($td_post_theme_settings[\'td_primary_cat\']);
} else {
//get one auto
$categories = get_the_category($this->post->ID);
if (is_category()) {
foreach ($categories as $category) {
if ($category->term_id == get_query_var(\'cat\')) {
$selected_category_obj = $category;
break;
}
}
}
if (empty($selected_category_obj) and !empty($categories[0])) {
if ($categories[0]->name === TD_FEATURED_CAT and !empty($categories[1])) {
$selected_category_obj = $categories[1];
} else {
$selected_category_obj = $categories[0];
}
}
}
if (!empty($selected_category_obj)) {
$selected_category_obj_id = $selected_category_obj->cat_ID;
$selected_category_obj_name = $selected_category_obj->name;
}
} else {
// custom post type
// Validate that the current queried term is a term
global $wp_query;
$current_queried_term = $wp_query->get_queried_object();
if ( $current_queried_term instanceof WP_Term ) {
$current_term = term_exists( $current_queried_term->name, $current_queried_term->taxonomy );
if ($current_term !== 0 && $current_term !== null) {
$selected_category_obj = $current_queried_term;
}
}
// Get and validate the custom taxonomy according to the validated queried term
if (!empty($selected_category_obj)) {
$taxonomy_objects = get_object_taxonomies($this->post, \'objects\');
$custom_taxonomy_object = \'\';
foreach ($taxonomy_objects as $taxonomy_object) {
if ($taxonomy_object->_builtin !== 1 && $taxonomy_object->name === $selected_category_obj->taxonomy) {
$custom_taxonomy_object = $taxonomy_object;
break;
}
}
// Invalid taxonomy
if (empty($custom_taxonomy_object)) {
return $buffy;
}
$selected_category_obj_id = $selected_category_obj->term_id;
$selected_category_obj_name = $selected_category_obj->name;
}
}
if (!empty($selected_category_obj_id) && !empty($selected_category_obj_name)) { //@todo catch error here
$buffy .= \'<a href="\' . get_category_link($selected_category_obj_id) . \'" class="td-post-category">\' . $selected_category_obj_name . \'</a>\' ;
}
//return print_r($post, true);
return $buffy;
}