这应该会为你找到一位直系父母。
// get the current category ID
$category_id = get_the_category(get_the_ID());
// get the current category object
$child = get_category($category_id[0]->term_id);
// get it\'s parent object
$parent = get_category($child->parent);
var_dump($parent->name . \' - \' . $child->name);
相同,但在Wordpress循环中:
$args = [
\'post_type\' => \'post\'
];
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
// get the current category ID
$category_id = get_the_category(get_the_ID());
// get the current category object
$child = get_category($category_id[0]->term_id);
// get it\'s parent object
$parent = get_category($child->parent);
dump(get_the_title() . \': \' . $parent->name . \' - \' . $child->name);
}
}