admin管理员组

文章数量:1389758

I have created the custom taxonomy for categories. I want to list the child categories lists of current parent category in child page.

$cateID = get_queried_object_id();    
$args = array(
  'format' => 'name',
  'separator' => ' ',
  'link' => false,
  'inclusive' => true
);
$checkparent =  get_term_parents_list( 
 $cateID, 'my-taxonamy' , $args
); 

I have tried the above code. But the code returns the parent category name/link. Any way to get the parent category id in child category page? Any methods or hooks are there in wordpress for getting the parent category id?

Example case:

  1. Parent category1
    • Child category1
    • Child category1
    • Child category3
  2. Parent category1
    • Child category1
    • Child category1

If i am in child category1 page, want to list the below categories.

  • Child category1
  • Child category1
  • Child category3

Problem: I cannot able to get the parent category id from the child category.

Thanks in advance ;)

I have created the custom taxonomy for categories. I want to list the child categories lists of current parent category in child page.

$cateID = get_queried_object_id();    
$args = array(
  'format' => 'name',
  'separator' => ' ',
  'link' => false,
  'inclusive' => true
);
$checkparent =  get_term_parents_list( 
 $cateID, 'my-taxonamy' , $args
); 

I have tried the above code. But the code returns the parent category name/link. Any way to get the parent category id in child category page? Any methods or hooks are there in wordpress for getting the parent category id?

Example case:

  1. Parent category1
    • Child category1
    • Child category1
    • Child category3
  2. Parent category1
    • Child category1
    • Child category1

If i am in child category1 page, want to list the below categories.

  • Child category1
  • Child category1
  • Child category3

Problem: I cannot able to get the parent category id from the child category.

Thanks in advance ;)

Share Improve this question edited Mar 5, 2020 at 13:51 Rajkumar S asked Mar 5, 2020 at 13:40 Rajkumar SRajkumar S 31 silver badge5 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

On a taxonomy archive page, be it for a child or parent term, you can get the current/queried term object/data using get_queried_object() which contains properties like term_id (the term ID) and slug (the term slug). And the property name for the ID of the parent term is parent. So you can do so to get the parent's term ID:

$cat = get_queried_object();
$parent_cat_id = $cat->parent;

And for displaying a list of terms in the parent term, you can use wp_list_categories(). Here's an example with the title_li set to '' and echo set to 0, which means I'm manually putting the output into an UL (<ul></ul>):

$cat = get_queried_object();

$list = wp_list_categories( [
    'taxonomy' => $cat->taxonomy,
    'child_of' => $cat->parent,
    'title_li' => '',
    'echo'     => 0,
] );

if ( $list ) {
    echo "<ul>$list</ul>";
}

If you want full control over the HTML, e.g. to add custom HTML before/after the term link or perhaps to add custom CSS classes, you can use get_terms() and loop through the term objects to display the term:

$cat = get_queried_object();

$cats = get_terms( [
    'taxonomy' => $cat->taxonomy,
    'child_of' => $cat->parent,
] );

if ( ! empty( $cats ) ) {
    echo '<ul>';
    foreach ( $cats as $cat ) {
        $url = esc_url( get_category_link( $cat ) );
        // change the 'before' and/or 'after' or whatever necessary
        echo "<li>before <a href='$url'>$cat->name</a> after</li>";
    }
    echo '</ul>';
}

Using a get_term function is Retrieve the terms in a given taxonomy or list of taxonomies.

Refer this Link : https://developer.wordpress/reference/functions/get_terms/

     /*change on 'taxonomy'=> 'your texonomy name'*/
      $terms = get_terms(array('taxonomy'=> 'category','hide_empty' => false, ));  ?>
        <div class="category_all">
            <div class="categor">
                <?php 
                echo "<ol>";
                 //get parent category term id $term->term_id            
                foreach ( $terms as $term ) {
                    if ($term->parent == 0 ) {
                     echo "<li>";                       
                     echo $term->name; // Parent Category Name 
                         echo "<ul>";
                         $subterms = get_terms(array('taxonomy'=> 'category','hide_empty' => false,'parent'=> $term->term_id));

                        //get child category term id $value->term_id
                        foreach ($subterms as $key => $value) 
                        {
                           echo "<li>".$value->name."</li>"; // Child Category Name 
                        }
                        echo "</ul>";                       
                    echo "</li>";
                    }                   

                    }
                    echo "</ol>";
            ?>
        </div>
    </div>

<?php

If you want to get category term id then you can use $term->term_id in your expression

Screenshot of parent with child Category

本文标签: categoriesGet parent category id from child category page for custom taxonomy