function last_cat($post_id = null) {
    // Get the terms of the custom taxonomy for the current post type
    $terms = get_terms(array(
        'taxonomy' => 'qqcpat',
        'object_ids' => $post_id,
        'hide_empty' => false,
    ));

    // Check if there are any terms
    if (!empty($terms)) {
        // Get the last child term
        $last_child_term = null;

        // Loop through the terms to find the last child term
        foreach ($terms as $term) {
            if ($term->parent == 0) {
                $last_child_term = $term;
            } else {
                $parent_term = get_term($term->parent, 'qqcpat');
                if ($parent_term && $parent_term->parent == 0) {
                    $last_child_term = $term;
                }
            }
        }

        // Output the last child term
        if ($last_child_term) {
            echo $last_child_term->name;
        }
    }
}
Get the Last Child Taxonomy Term in WordPress

原文地址: https://www.cveoy.top/t/topic/qjcT 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录