function get_post_category($postId) {
global $wpdb;
if (!is_null($postId)) {
$conditions = "AND p.id=" . $postId;
} else {
return false;
}
$conditions .= " AND ttax.taxonomy=\'category\'";
$selectfields = "term.name name";
$query = "SELECT " . $selectfields . "
FROM wp_posts p
INNER JOIN wp_term_relationships trel ON trel.object_id = p.id
INNER JOIN wp_term_taxonomy ttax ON ttax.term_taxonomy_id = trel.term_taxonomy_id
INNER JOIN wp_terms term ON term.term_id = ttax.term_id
WHERE 1=1 " . $conditions ." LIMIT 1";
return $wpdb->get_results($query);
}
此函数需要帖子id作为参数,它将为您提供帖子的最顶层类别,如果您取消限制,它将为您提供帖子所有类别的列表。我不确定你是否想达到同样的目标。