W
W
William2020-04-27 01:41:33
WordPress
William, 2020-04-27 01:41:33

How to get the id of the highest parent term in the page of the current term?

Custom taxonomy term list:

Category 1
-subcategory
--subsubcategory (located here)

code is:

$term_id = get_queried_object_id();
    $child_term = get_term( $term_id, 'my_taxonomy' );
    $term_parent_id = $child_term->parent;


using echo $term_parent_id - get id -subcategory
and how to get id Category 1 ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
William, 2020-04-27
@william666

issue resolved :)

$term_id = get_queried_object_id();
$ancestor_cat_ids = get_ancestors( $term_id, 'my_taxonomy');
$term_parent_id = $ancestor_cat_ids[count($ancestor_cat_ids) - 1];

 echo 'вот та самая id самого верхнего уровня родителя' . $term_parent_id . '';

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question